如果一个文件夹里包含有子文件夹 , 子文件夹里还有子文件夹...如何用C#统计所有子文件夹的个数并且遍历出所有的文件夹!?

解决方案 »

  1.   

    遍历
    http://www.cnblogs.com/qianqianfy/archive/2009/07/08/1518974.html至于个数,可以用个计数器
      

  2.   

    public   void   FindFile(string   dir)                    
      {         
      DirectoryInfo   Dir=new   DirectoryInfo(dir);   
      try   
      {   
      foreach(DirectoryInfo   d   in   Dir.GetDirectories())      
      {   
      FindFile(Dir+d.ToString()+"\\");   
      }   
     foreach(FileInfo   f   in   Dir.GetFiles("*.*"))              
      {   
       
      }   
      }   
      catch(Exception   e)   
      {   
      MessageBox.Show(e.Message);   
      }   
      }   
     
      
    foreach (string dir in System.IO.Directory.GetDirectories("最上级文件夹全路径", "*.*", SearchOption.AllDirectories))
                {
                    System.IO.DirectoryInfo di = new DirectoryInfo(dir);
                    if (di.GetFiles().Length>0)
                        {//递归}
                }
      

  3.   


      DirectoryInfo di = new DirectoryInfo(@"c:\MyDir");
            try 
            {
                // Determine whether the directory exists.
                if (di.Exists) 
                {
                    // Indicate that the directory already exists.
                    Console.WriteLine("That path exists already.");
                    return;
                }            // Try to create the directory.
                di.Create();
                Console.WriteLine("The directory was created successfully.");            // Delete the directory.
                di.Delete();
                Console.WriteLine("The directory was deleted successfully.");        } 
            catch (Exception e) 
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            } 
            finally {}
      

  4.   

    int k = 0;
    public void GetDirectorys(rootdirectory)
    {
    foreach(DirectoryInfo i in rootdirectory)
    {
    k++;
    if(i.GetDirectories().Length != 0)
    {
    GetDirectorys(i);
    }
    }
    }
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;namespace _4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private string AddNote(DirectoryInfo dd,TreeNode nodes)
            {            try
                {
                    FileInfo[] infos = dd.GetFiles("*.*");
                    foreach (FileInfo file in infos)
                    {
                        nodes.Nodes.Add(file.Name);
                    }
                    DirectoryInfo[] dir = dd.GetDirectories();
                    foreach (DirectoryInfo d in dir)
                    {                    nodes.Nodes.Add(d.Name);
                        AddNote(d, nodes.Nodes[nodes.Nodes.Count - 1]);
                    }
                }
                catch 
                {
                }
                return "";
            }        private void Form1_Load(object sender, EventArgs e)
            {
                MessageBox.Show("如果你C盘文件较多,电脑很烂,本程序打开时间(<30s)可能会过长请耐心等待。");
                this.treeView1.Nodes.Add("c:\\");
                DirectoryInfo ddd = new DirectoryInfo("c:\\");
                AddNote(ddd, this.treeView1.Nodes[0]);
                
            }        private void treeView1_DoubleClick(object sender, EventArgs e)
            {
                if (this.treeView1.SelectedNode.Nodes.Count > 0)
                {
                    
                    this.treeView1.SelectedNode.Expand();
                }
               
            }        //控制contextMenuStrip的可用性        private void treeView1_MouseClick(object sender, MouseEventArgs e)
            {
                if (this.treeView1.SelectedNode.Nodes.Count > 0)
                {
                    this.treeView1.SelectedNode.ContextMenuStrip = this.contextMenuStrip1;
                }
            }        private void 全部展开ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.treeView1.SelectedNode.ExpandAll();
            }        private void 全部摺叠ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.treeView1.SelectedNode.Toggle();
            }        private void 展开选中节点ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.treeView1.SelectedNode.Expand();
            }    }}