C#读取文件夹树结构
一级文件夹为主目录,1个,二级文件夹有好多个,三级文件夹有好多个,一直到8级文件夹,但是我只想读取到第5级文件夹,后面三级文件夹我不想读取出来。
代码如下,path为一级文件夹目录,怎么修改可以只读到5级文件夹就停止,而不是读完全部?(读出的结构是放在treeView里)
代码如下,path为一级文件夹目录,怎么修改可以只读到5级文件夹就停止,而不是读完全部?(读出的结构是放在treeView里)
public void listTreeNode(string path)
{
List<TreeNode> list = new List<TreeNode>();
DirectoryInfo dirInfo = new DirectoryInfo(path);
FileSystemInfo[] dir = dirInfo.GetFileSystemInfos();
foreach (FileSystemInfo di in dir)
{
if (di.Attributes == (System.IO.FileAttributes.Hidden | System.IO.FileAttributes.System | System.IO.FileAttributes.Directory))
{
continue;
}
TreeNode treeNode = new TreeNode(di.Name, ListFiles(di).ToArray());
list.Add(treeNode);
}
TreeNode node = new TreeNode(path, list.ToArray());
treeView1.Nodes.Add(node);
node.Expand();
}
public List<TreeNode> ListFiles(FileSystemInfo fileinfo)
{
List<TreeNode> nodeList = new List<TreeNode>();
if (!fileinfo.Exists)
return null;
DirectoryInfo dirinfo = fileinfo as DirectoryInfo;
if (dirinfo == null)
return null;
FileSystemInfo[] files = dirinfo.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
DirectoryInfo subdir = files[i] as DirectoryInfo;
if (subdir == null)
{
continue;
}
TreeNode tnode = new TreeNode(subdir.Name, ListFiles(files[i]).ToArray());
nodeList.Add(tnode);
}
return nodeList;
}
...全文
相关推荐
C# D3.js 树形文件夹管理器
使用C#,cefsharp和d3.js开发的树形文件夹管理器,能够快速整理修改和查看文件夹内所有的子文件。
C#递归遍历文件夹显示在TreeView
C#递归遍历文件夹显示在TreeView
C#读取文件夹下所有文件名称,大小及其它属性
摘要:C#源码,文件操作,读取文件夹 C#读取文件夹下所有文件的属性信息,包括文件名称,文件大小,创建时间、路径等。实现思路:实例化DirectoryInfo对象,获取指定目录下的所有子目录及文件类型,/判断是否是文件夹,使用获取的文件夹名称实例化DirectoryInfo对象,为ListView控件添加文件夹信息,以列表形式显示文件夹的名称、文件大小和路径等,然后使用获取的文件名称实例化FileInfo对象,为ListView控件添加文件信息等。
c# 创建文件夹 文件 写入文件 读取文件 读取图片
c# winform
C#读取某个文件夹列表
C#读取某个文件夹列表。需要看例子就下载看看吧


