【问题标题】:C# dropbox of drivesC# 驱动器保管箱
【发布时间】:2009-03-08 06:54:43
【问题描述】:

我记得在 vb6 中有一个类似于下拉框/组合框的控件,您可以选择驱动器名称。它引发一个事件,然后您可以设置另一个控件来枚举列表框中的文件。 (在 drive.event 你做 files.path = drive.path 来获得这种影响)。

C#中有这样的东西吗?下拉可用驱动器列表并在更改时引发事件的控件?

【问题讨论】:

  • 您需要能够列出系统可用的驱动器是有正当理由的:例如,我们需要能够配置系统以根据用户使用的驱动器收取不同的金额选择保存到。 (不,我认为这不是一个好主意,但客户坚持)。

标签: c# drive


【解决方案1】:

没有内置控件可以做到这一点,但使用标准 ComboBox 很容易实现。在表单上放置一个,将其 DropDownStyle 更改为 DropDownList 以防止编辑,并在表单的 Load 事件中添加以下行:

comboBox1.DataSource = Environment.GetLogicalDrives();

现在您可以处理 SelectedValueChanged 事件,以便在有人更改所选驱动器时采取行动。

在回答this question 之后,我找到了另一种(更好的?)方法来做到这一点。您可以使用 DriveInfo.GetDrives() 方法枚举驱动器并将结果绑定到 ComboBox。这样您就可以限制出现的驱动器。所以你可以从这个开始:

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 方法要灵活得多。

【讨论】:

  • 奇怪的是,这给了我“在 mscorlib.dll 中发生了 'System.UnauthorizedAccessException' 类型的第一次机会异常” 所以如果我在 try catch 中使用它,我想它会是更好。
【解决方案2】:

虽然马特汉密尔顿的回答非常正确,但我想知道问题本身是否正确。因为,你为什么想要这样的控制?老实说,感觉很Windows 95。请查看 Windows 用户体验交互指南:http://msdn.microsoft.com/en-us/library/aa511258.aspx

特别是关于常用对话框的部分: http://msdn.microsoft.com/en-us/library/aa511274.aspx

【讨论】:

  • 这是我过去使用的一个解决方案,我害怕模仿我习惯于在 c++ 中这样做的功能 :(。我也是 C# 的新手,从没想过答案就像马特解决方案一样简单
【解决方案3】:

我会这样做:

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

Drive.IsReady 的帮助下,您可以避免遇到DeviceNotReadyDeviceUnavailable 问题。

奖励: 这里还有一个简单的“ChooseFile”示例,其中包括一个用于驱动器的ComboBox,一个用于文件夹的TreeView,最后一个用于文件的ListBox

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 中的旧时代一样。

【讨论】:

    【解决方案4】:

    一个组合框和这个小代码就可以完成这项工作。最亲切的问候!路易斯:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 2018-08-10
      • 2012-08-19
      相关资源
      最近更新 更多