C#实现的列出目录下所有子目录和文件的程序(附思路)

C#实现的列出目录下所有子目录和文件的程序(附思路)


作者: fanz2000  &;nbsp来自:网络


                把自己的资料刻录成很多光盘,发现连自己都很难找到需要的文件在哪张光盘上,因此我就根据需求,在Visual Studio.NET中写了一个列出目录下所有子目录和文件的程序,以方便我列出刻录的资料光盘上的所有文件信息。
 
  本程序的主要算法是递归,主函数如下:
 
  //递归列出目录下的所有文件和子目录
 
                    public void ListFiles( FileSystemInfo fileinfo )
 
                    {
 
                              if( ! fileinfo.Exists ) return;
 
                              DirectoryInfo dirinfo = fileinfo as DirectoryInfo;
 
                              if( dirinfo == null ) return; //不是目录
 
                              indent  ;//缩进加一
 
                              FileSystemInfo [] files = dirinfo.GetFileSystemInfos();
 
                              for( int i=0; i< i  )>遍历目录下所有文件、子目录
 
                              {
 
                                      FileInfo file = files as FileInfo;
 
                                      if( file != null ) // 是文件
 
                                      {
 
                                                this.richTextBox1.Text =(WriteSpace(indent) "|-"
 
                                                          file.Name  "\t"  ConvertToKByte(file.Length) "\r" );
 
                                      }
 
                                      else  //是目录
 
                                      {
 
                                                this.richTextBox1.Text =(WriteSpace(indent) " " files.FullName "\r");
 
                                                ListFiles( files );  //对子目录进行递归调用
 
                                      }
 
                              }
 
                              indent--;//缩进减一
 
                    }
 
  程序的设计界面如下图所示:
 
  控件有两个Button控件btnSelect和btnSave(分别用来选择目录和保存文件);一个RichTextBox控件(显示结果),一个folderBrowserDialog控件(选择目录)和一个saveFileDialog控件(选择保存文件路径)。
 
  程序运行后的界面如下图所示:
 
 
 
 
 
  程序的完整代码如下:(其中红色的是我自己添加的)
 
  using System;
 
  using System.Drawing;
 
  using System.Collections;
 
  using System.ComponentModel;
 
  using System.Windows.Forms;
 
  using System.Data;
 
  using System.Globalization;
 
  using System.IO;
 
 
 
  namespace ListFile_Windows
 
  {
 
          ///
 
          /// Form1 的摘要说明。
 
          ///
 
          public class Form1 : System.Windows.Forms.Form
 
          {
 
                    private System.Windows.Forms.RichTextBox richTextBox1;
 
                    public static int indent; //缩进值
 
                    private System.Windows.Forms.Button btnSelect;
 
                    private System.Windows.Forms.Button btnSave;
 
                    private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
 
                    private System.Windows.Forms.SaveFileDialog saveFileDialog1;
 
                    ///
 
                    /// 必需的设计器变量。
 
                    ///
 
                    private System.ComponentModel.Container components = null;
 
 
 
                    public Form1()
 
                    {
 
                              //
 
                              // Windows 窗体设计器支持所必需的
 
                              //
 
                              InitializeComponent();
 
 
 
                              //
 
                              // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 
                              //
 
                    }
 
 
 
                    ///
 
                    /// 清理所有正在使用的资源。
 
                    ///
 
                    protected override void Dispose( bool disposing )
 
                    {
 
                              if( disposing )
 
                              {
 
                                      if (components != null)
 
                                      {
 
                                                components.Dispose();
 
                                      }
 
                              }
 
                              base.Dispose( disposing );
 
                    }
 
 
 
                    #region Windows 窗体设计器生成的代码
 
                    ///
 
                    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 
                    /// 此方法的内容。
 
                    ///
 
                    private void InitializeComponent()
 
                    {
 
                              this.richTextBox1 = new System.Windows.Forms.RichTextBox();
 
                              this.btnSelect = new System.Windows.Forms.Button();
 
                              this.btnSave = new System.Windows.Forms.Button();
 
                              this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
 
                              this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
 
                              this.SuspendLayout();
 
                              //
 
                              // richTextBox1
 
                              //
 
                              this.richTextBox1.Location = new System.Drawing.Point(0, 0);
 
                              this.richTextBox1.Name = "richTextBox1";
 
                              this.richTextBox1.Size = new System.Drawing.Size(528, 400);
 
                              this.richTextBox1.TabIndex = 0;
 
                              this.richTextBox1.Text = "";
 
                              //
 
                              // btnSelect
 
                              //
 
                              this.btnSelect.Location = new System.Drawing.Point(112, 424);
 
                              this.btnSelect.Name = "btnSelect";
 
                              this.btnSelect.TabIndex = 1;
 
                              this.btnSelect.Text = "选择目录";
 
                              this.btnSelect.Click  = new System.EventHandler(this.btnSelect_Click);
 
                              //
 
                              // btnSave
 
                              //
 
                              this.btnSave.Location = new System.Drawing.Point(320, 424);
 
                              this.btnSave.Name = "btnSave";
 
                              this.btnSave.TabIndex = 2;
 
                              this.btnSave.Text = "保存文件";
 
                              this.btnSave.Click  = new System.EventHandler(this.btnSave_Click);
 
                              //
 
                              // Form1
 
                              //
 
                              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
 
                              this.ClientSize = new System.Drawing.Size(528, 461);
 
                              this.Controls.Add(this.btnSave);
 
                              this.Controls.Add(this.btnSelect);
 
                              this.Controls.Add(this.richTextBox1);
 
                              this.Name = "Form1";
 
                              this.Text = "Form1";
 
                              this.ResumeLayout(false);
 
 
 
                    }
 
                    #endregion
 
 
 
                    ///
 
                    /// 应用程序的主入口点。
 
                    ///
 
                    [STAThread]
 
                    static void Main()
 
                    {
 
                              Application.Run(new Form1());
 
                    }
 
 
 
          //递归列出目录下的所有文件和子目录
 
                    public void ListFiles( FileSystemInfo fileinfo )
 
                    {
 
                              if( ! fileinfo.Exists ) return;
 
                              DirectoryInfo dirinfo = fileinfo as DirectoryInfo;
 
                              if( dirinfo == null ) return; //不是目录
 
                              indent  ;//缩进加一
 
                              FileSystemInfo [] files = dirinfo.GetFileSystemInfos();
 
                              for( int i=0; i< i  )>遍历目录下所有文件、子目录
 
                              {
 
                                      FileInfo file = files as FileInfo;
 
                                      if( file != null ) // 是文件
 
                                      {
 
                                                this.richTextBox1.Text =(WriteSpace(indent) "|-"
 
                                                          file.Name  "\t"  ConvertToKByte(file.Length) "\r" );
 
                                      }
 
                                      else  //是目录
 
                                      {
 
                                                this.richTextBox1.Text =(WriteSpace(indent) " " files.FullName "\r");
 
                                                ListFiles( files );  //对子目录进行递归调用
 
                                      }
 
                              }
 
                              indent--;//缩进减一
 
                    }
 
                   
 
                    //控制缩进空格,n为空格数
 
                    public  string WriteSpace(int n)
 
                    {
 
                              string strspace="";
 
                              for(int i=1;i<=n;i  )
 
                                      strspace ="  ";
 
                              return strspace;
 
                    }
 
                   
 
                    //显示文件字节数
 
                    public string ConvertToKByte(long len)
 
                    {
 
                              float val;
 
                              NumberFormatInfo myNfi = new NumberFormatInfo();
 
                              myNfi.NumberDecimalDigits=1; //显示一位小数
 
 
 
                              if(len/1024==0)
 
                                      return len.ToString() "字节";
 
                              if(len/1024/1024==0)
 
                              {
 
                                      val=(float)len/1024;
 
                                      return val.ToString("N",myNfi) "K字节";
 
                              }
 
                              val=(float)len/1024/1024;
 
                              return val.ToString("N",myNfi) "M字节";
 
 
 
                    }
 
 
 
                    private void btnSelect_Click(object sender, System.EventArgs e)
 
                    {
 
                              indent=0;//缩进清零
 
                              this.richTextBox1.ResetText(); //清空文本框中的原来的文本
 
  //选择目录
 
                              if(this.folderBrowserDialog1.ShowDialog()==DialogResult.OK)
 
                              {
 
                                      ListFiles(new DirectoryInfo(this.folderBrowserDialog1.SelectedPath));
 
                              }
 
                    }
 
 
 
                    private void btnSave_Click(object sender, System.EventArgs e)
 
                    {
 
                              if(this.saveFileDialog1.ShowDialog()==DialogResult.OK)
 
                              {
 
                              //保存结果文件
 
                              this.richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);
 
                           
 
                              }
 
                    }
 
 
 
          }
  }
  作者Blog:http://blog.csdn.net/fanz2000/  (2005-8-05:12:36)
 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 警告:持续变种木马正在发起农历新年攻势!
 您可能对 [C#] 的这些文章也感兴趣:
c#中邮件收发处理(POP3,IMAP,SMTP)
C#的Hotkey简单封装
在C#中调用VC编写的dll库
C#操作RTF文档
C#和VB.NET版CLSA.NET 3.0电子图书面市
SmartSoft中用C#.Net实现AutoCAD块属性提取
C#编程中的 New 关键词的几种用法
winform绘图技术
C#实现Windows 服务的制作、安装和删除
C# 中的 @ 符号的使用及注意事项
C#生成bt种子
C#开源项目