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

类别:.NET开发 点击:0 评论:0 推荐:

把自己的资料刻录成很多光盘,发现连自己都很难找到需要的文件在哪张光盘上,因此我就根据需求,在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遍历目录下所有文件、子目录

                            {

                                     FileInfo file = files[i] as FileInfo;

                                     if( file != null ) // 是文件

                                     {

                                               this.richTextBox1.Text+=(WriteSpace(indent)+"|-"+

                                                        file.Name + "\t" + ConvertToKByte(file.Length)+"\r" );

                                     }

                                     else   //是目录

                                     {

                                               this.richTextBox1.Text+=(WriteSpace(indent)+"+"+files[i].FullName+"\r");

                                               ListFiles( files[i] );  //对子目录进行递归调用

                                     }

                            }

                            indent--;//缩进减一

                   }

程序的设计界面如下图所示:

控件有两个Button控件btnSelectbtnSave(分别用来选择目录和保存文件);一个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遍历目录下所有文件、子目录

                            {

                                     FileInfo file = files[i] as FileInfo;

                                     if( file != null ) // 是文件

                                     {

                                               this.richTextBox1.Text+=(WriteSpace(indent)+"|-"+

                                                        file.Name + "\t" + ConvertToKByte(file.Length)+"\r" );

                                     }

                                     else   //是目录

                                     {

                                               this.richTextBox1.Text+=(WriteSpace(indent)+"+"+files[i].FullName+"\r");

                                               ListFiles( files[i] );  //对子目录进行递归调用

                                     }

                            }

                            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);

                           

                            }

                   }

 

         }

}

本文地址:http://com.8s8s.com/it/it44282.htm