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

把自己的资料刻录成很多光盘,发现连自己都很难找到需要的文件在哪张光盘上,因此我就根据需求,在 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 控件 btnSelect 和 btnSave (分别用来选择目录和保存文件);一个 RichTextBox 控件(显示结果),一个 folderBrowserDialog 控件(选择目录)和一个 saveFileDialog 控件(选择保存文件路径)。

程序运行后的界面如下图所示:

![](http://dev.csdn.net/article/35/C:/Documents and Settings\Administrator\桌面\ListFiles说明.files\image004.jpg)

程序的完整代码如下:(其中红色的是我自己添加的)

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( fi

Published At
Categories with Web编程
Tagged with
comments powered by Disqus