C#驱动器的升降箱
我记得在vb6中有一个类似于dropbox/combobox的控件,可以选择驱动器名。它引发一个事件,然后您可以设置另一个控件来枚举listbox中的文件。(在drive.event中,您可以使用files.path=drive.path来获得此效果)C#驱动器的升降箱,c#,drive,C#,Drive,我记得在vb6中有一个类似于dropbox/combobox的控件,可以选择驱动器名。它引发一个事件,然后您可以设置另一个控件来枚举listbox中的文件。(在drive.event中,您可以使用files.path=drive.path来获得此效果) 在C#中有类似的东西吗?下拉可用驱动器列表并在更改时引发事件的控件?没有内置控件来执行此操作,但使用标准组合框很容易实现。在表单上放置一个,将其DropDownStyle更改为DropDownList以防止编辑,并在表单的加载事件中添加以下行:
在C#中有类似的东西吗?下拉可用驱动器列表并在更改时引发事件的控件?没有内置控件来执行此操作,但使用标准组合框很容易实现。在表单上放置一个,将其DropDownStyle更改为DropDownList以防止编辑,并在表单的加载事件中添加以下行:
comboBox1.DataSource = Environment.GetLogicalDrives();
现在,您可以处理SelectedValueChanged事件,以便在有人更改所选驱动器时采取操作
回答之后,我找到了另一种(更好的?)方法。您可以使用DriveInfo.GetDrives()方法枚举驱动器并将结果绑定到组合框。这样您就可以限制哪些驱动程序。所以你可以从这个开始:
comboBox1.DataSource = System.IO.DriveInfo.GetDrives();
comboBox1.DisplayMember = "Name";
现在comboBox1.SelectedValue将是DriveInfo类型,因此您将获得有关所选游戏的更多信息。如果您只想显示网络驱动器,现在可以执行以下操作:
comboBox1.DataSource = System.IO.DriveInfo.GetDrives()
.Where(d => d.DriveType == System.IO.DriveType.Network);
comboBox1.DisplayMember = "Name";
我认为DriveInfo方法更加灵活。虽然马特·哈密尔顿的答案非常正确,但我想知道问题本身是否正确。因为,你为什么想要这样的控制?老实说,感觉很好。请查看Windows用户体验交互指南: 特别是关于常见对话框的部分:
我会用以下方法来解决这个问题:
foreach (var Drives in Environment.GetLogicalDrives())
{
DriveInfo DriveInf = new DriveInfo(Drives);
if (DriveInf.IsReady == true)
{
comboBox1.Items.Add(DriveInf.Name);
}
}
借助Drive.IsReady
u可以避免出现DeviceTready
或DeviceUnavailable
问题
奖金:
这里还有一个简单的“ChooseFile”示例,其中包括驱动器的组合框
,文件夹的树状视图
,文件的最后一个列表框
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());
}
}
}
}
就像VB6中的旧时代一样。一个组合框,这段小代码就可以完成这项工作。最亲切的问候!路易斯:
comboBox1.DataSource = System.IO.DriveInfo.GetDrives()
.Where(d => d.DriveType == System.IO.DriveType.Network).ToList();
comboBox1.DisplayMember = "Name";
它是我过去使用的一种解决方案,我害怕模仿我在C++中使用的功能。(.另外,我在C#还是个新手,从来没有想到答案会像Matt Solution那么简单。您需要列出系统可用驱动器的原因是有道理的:例如,我们需要能够配置一个系统,根据用户选择保存到哪个驱动器来收取不同金额的费用。(不,我认为这不是一个好主意,但客户坚持认为)。奇怪的是,这给了我“mscorlib.dll中发生了“System.UnauthorizedAccessException”类型的第一次机会异常”,所以如果我在try catch中使用它,我想会更好。