C#驱动器保管箱

问题描述 投票:4回答:4

我记得在vb6中有一个类似于Dropbox / Comboxbox的控件,您可以选择驱动器名称。它引发一个事件,您可以随后设置另一个枚举列表框中文件的控件。 (在drive.event中,您执行files.path = drive.path来获得此影响)。

C#中是否有类似的东西?一个控件,该控件会下拉可用驱动器列表并在更改时引发事件?

c# drive
4个回答
13
投票

没有内置控件可以执行此操作,但是使用标准的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方法要灵活得多。


3
投票

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

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


1
投票

我可以通过:

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


0
投票

一个comboBox和这个小代码将完成这项工作。最亲切的问候!路易斯:

    comboBox1.DataSource = System.IO.DriveInfo.GetDrives()
        .Where(d => d.DriveType == System.IO.DriveType.Network).ToList();
    comboBox1.DisplayMember = "Name";
© www.soinside.com 2019 - 2024. All rights reserved.