且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用c#windows的目录文件和文件夹列表

更新时间:2022-03-19 01:49:13

DirectoryInfo [ ^ ]类提供您所需要的一切,即 GetFiles GetDirectories 方法和属性。
The DirectoryInfo[^] class provides all you need, namely the GetFiles, GetDirectories methods and the Parent property.


使用 System.IO.Directory.GetFiles [ ^ ]和获取目录 [ ^ ]检索信息的方法。

然后你可以在DataGridView [ ^ ]就像任何其他字符串一样。
Use the System.IO.Directory.GetFiles[^] and GetDirectories[^] methods to retrieve the information.
Then you can display it in a DataGridView[^] just like any other pile of strings.
MyDataGridView.Rows[whateverRowNumber].Columns["ChildFolderNameColumn"].Value = childFolderName;



或者您可以将数据存储在 BindingList 中[ ^ ]并将其绑定到DataGridView。

根据您的具体情况,它可以比第一个选项更轻松。


Or you can store your data in a BindingList[^] and bind it to the DataGridView.
Depending on your very situation, it can be less hassle than the first option.


private void btnGetFolders_Click(object sender, EventArgs e)
        {
            string[] dirs = Directory.GetDirectories(@"DirectoryPath eg. E:\Music");
            
            foreach (string dir in dirs)
            {
                lstMusic.Items.Add(dir); //Adding the list of files and folders to a listbox called lstMusic           
            }

        }