使用c#windows的目录文件和文件夹列表 [英] List of files and folders of a directory using c# windows

查看:65
本文介绍了使用c#windows的目录文件和文件夹列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何使用c #windows以如下所示的形式获取网格视图中目录的所有文件和文件夹列表。

Hi,

How can I get a list of all files and folders of a directory in a grid view in the form as shown below using c# windows .

ParentFolder     ChildFolder     Filename
AAAA             aaaaaa          aaa.txt





谢谢

Rahul



Thanks
Rahul

推荐答案

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


使用< a href =http://msdn.microsoft.com/en-us/library/07wt70x2.aspx> 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           
            }

        }


这篇关于使用c#windows的目录文件和文件夹列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆