码迷,mamicode.com
首页 > 其他好文 > 详细

使用TreeView加载文件夹

时间:2019-03-01 15:53:17      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:files   new   使用   tostring   director   button   start   proc   system   

    String Path = "C:\\Test";

    private void ListDirectory(TreeView treeView, string path)
    {
        treeView.Nodes.Clear();
        var rootDirectoryInfo = new DirectoryInfo(path);

        treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
    }

    private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
    {
        var directoryNode = new TreeNode(directoryInfo.Name);
        foreach (var directory in directoryInfo.GetDirectories())
            directoryNode.Nodes.Add(CreateDirectoryNode(directory));

        foreach (var file in directoryInfo.GetFiles())
            directoryNode.Nodes.Add(new TreeNode(file.Name) { Tag = file });

        return directoryNode;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //string s = treeView1.SelectedNode.ToString();
        //String TreeNodeName = treeView1.SelectedNode.ToString().Replace("TreeNode: ", String.Empty);
        //System.Diagnostics.Process.Start(Path + "\\" + TreeNodeName);
        string p = (treeView1.SelectedNode?.Tag as FileInfo)?.FullName;
        if (p != null)
        {
            System.Diagnostics.Process.Start(p);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        ListDirectory(treeView1, Path);
    }

使用TreeView加载文件夹

标签:files   new   使用   tostring   director   button   start   proc   system   

原文地址:https://www.cnblogs.com/jizhiqiliao/p/10456560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!