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

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

猜你喜欢

转载自www.cnblogs.com/HarryChis/p/10460074.html