使用递归列出文件夹目录及目录下的文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace 使用递归列出文件夹目录及目录下的文件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string path = "D:\\Notes";
            TreeNode node = treeView1.Nodes.Add(path);
            DirectoryToTree(path, node.Nodes);
        }

        private void DirectoryToTree(string path, TreeNodeCollection nodes)
        {
            foreach (string item in Directory.GetDirectories(path))
            {
                TreeNode node = nodes.Add(Path.GetFileName(item));
                DirectoryToTree(item, node.Nodes);

            }
            string[] strFiles = Directory.GetFiles(path);
            foreach (string str in strFiles)
            {
                nodes.Add(Path.GetFileName(str));
            }
        }
    }
}

 

image

posted @ 2019-03-02 10:44  崇尚技术  阅读(524)  评论(0编辑  收藏  举报