TreeView获取目录下的所有文件
/// <summary>
/// TreeView獲取目錄下的所有文件
/// </summary>
/// <param name="tree"></param>
/// <param name="dirname"></param>
/// <returns></returns>
public static bool InitTreeView(TreeView tree, List<TreeNode> filenodes)
{
tree.Nodes.Clear();
foreach (TreeNode filenode in filenodes)
{
TreeNode root = new TreeNode(filenode.Name + "批次列表文件");
//if ((root = getRootNode(dirname)) == null)
// return false;
getRootNode(filenode.Tag.ToString(), root);
tree.Nodes.Add(root);
}
//root.Expand();
return true;
}
private static void getRootNode(string dirname, TreeNode treenode) //遞歸,返回根結點
{
treenode.Tag = dirname;
string[] dirs = Directory.GetDirectories(dirname);
string[] files = Directory.GetFiles(dirname);
foreach (string dir in dirs)
{
FileInfo fileinfo = new FileInfo(dir);
TreeNode node = new TreeNode(fileinfo.Name);
node.Tag = dir;
getRootNode(dir, node);
treenode.Nodes.Add(node);
}
foreach (string file in files)
{
FileInfo fileinfo1 = new FileInfo(file);
TreeNode fnode = new TreeNode(fileinfo1.Name);
fnode.Tag = file;
treenode.Nodes.Add(fnode);
}
}
轉載于:https://www.cnblogs.com/liuyongpingblog/p/7244388.html
總結
以上是生活随笔為你收集整理的TreeView获取目录下的所有文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux命令(2)vmstat
- 下一篇: 函数的闭包和装饰器