C # Dropbox Drives

I remember that in vb6 there was a control similar to dropbox / combobox that you can select a drive name. It raises an event that can then be set by another control that lists the files in the list. (in drive.event you do files.path = drive.path to get this effect).

Is there anything similar in C #? a control that omits the list of available disks and raises an event when it changes?

+4
source share
3 answers

There is no built-in control for this, but it is very easy to do with a standard ComboBox. Drop it in your form, change its DropDownStyle to DropDownList to prevent editing, and in the Load event for the form add the following line:

comboBox1.DataSource = Environment.GetLogicalDrives(); 

Now you can handle the SelectedValueChanged event to take action when someone changes the selected drive.

Answering this question , I found another way (better?). You can use the DriveInfo.GetDrives () method to list drives and bind the result to ComboBox. This way you can limit the use of disks. So you can start with this:

 comboBox1.DataSource = System.IO.DriveInfo.GetDrives(); comboBox1.DisplayMember = "Name"; 

Now comboBox1.SelectedValue will be of type DriveInfo, so you will get more information about the selected game. And if you want to show network drives, you can do it now:

 comboBox1.DataSource = System.IO.DriveInfo.GetDrives() .Where(d => d.DriveType == System.IO.DriveType.Network); comboBox1.DisplayMember = "Name"; 

I think the DriveInfo method is much more flexible.

+13
source

While Matt Hamiltons answer was very correct, I wonder if the question itself is. Because why do you need such control? Honestly, this is Windows 95. Please see the Windows User Engagement Guide: http://msdn.microsoft.com/en-us/library/aa511258.aspx

In particular, the section on general dialogs: http://msdn.microsoft.com/en-us/library/aa511274.aspx

+3
source

I would approach this with:

 foreach (var Drives in Environment.GetLogicalDrives()) { DriveInfo DriveInf = new DriveInfo(Drives); if (DriveInf.IsReady == true) { comboBox1.Items.Add(DriveInf.Name); } } 

Using Drive.IsReady u can avoid problems with DeviceNotReady or DeviceUnavailable .

Bonus: Also here is a simple "ChooseFile" example, which includes a ComboBox for drives, a TreeView for folders, and the last a ListBox for files.

 namespace ChosenFile { public partial class Form1 : Form { // Form1 FormLoad // public Form1() { InitializeComponent(); foreach (var Drives in Environment.GetLogicalDrives()) { DriveInfo DriveInf = new DriveInfo(Drives); if (DriveInf.IsReady == true) { comboBox1.Items.Add(DriveInf.Name); } } } // ComboBox1 (Drives) // private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedItem != null) { ListDirectory(treeView1, comboBox1.SelectedItem.ToString()); } } // ListDirectory Function (Recursive Approach): // private void ListDirectory(TreeView treeView, string path) { treeView.Nodes.Clear(); var rootDirectoryInfo = new DirectoryInfo(path); treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo)); } // Create Directory Node // private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo) { var directoryNode = new TreeNode(directoryInfo.Name); try { foreach (var directory in directoryInfo.GetDirectories()) directoryNode.Nodes.Add(CreateDirectoryNode(directory)); } catch (Exception ex) { UnauthorizedAccessException Uaex = new UnauthorizedAccessException(); if (ex == Uaex) { MessageBox.Show(Uaex.Message); } } return directoryNode; } // TreeView // private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { listBox1.Items.Clear(); listBox1.Refresh(); PopulateListBox(listBox1, treeView1.SelectedNode.FullPath.ToString(), "*.pdf"); } // PopulateListBox Function // private void PopulateListBox(ListBox lsb, string Folder, string FileType) { try { DirectoryInfo dinfo = new DirectoryInfo(Folder); FileInfo[] Files = dinfo.GetFiles(FileType); foreach (FileInfo file in Files) { lsb.Items.Add(file.Name); } } catch (Exception ex) { MessageBox.Show("An error occurred while attempting to load the file. The error is:" + System.Environment.NewLine + ex.ToString() + System.Environment.NewLine); } } // ListBox1 // private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (listBox1.SelectedItem != null) { //do smt here! MessageBox.Show(listBox1.SelectedItem.ToString()); } } } } 

Just like the old days in VB6.

+1
source

All Articles