• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • C#實現的列出目錄下所有子目錄和文件的程序(附思路)

    發表于:2007-06-30來源:作者:點擊數: 標簽:
    把自己的資料刻錄成很多光盤,發現連自己都很難找到需要的文件在哪張光盤上,因此我就根據 需求 ,在Visual Studio.NET中寫了一個列出目錄下所有子目錄和文件的程序,以方便我列出刻錄的資料光盤上的所有文件信息。 本程序的主要算法是遞歸,主函數如下: //
    把自己的資料刻錄成很多光盤,發現連自己都很難找到需要的文件在哪張光盤上,因此我就根據需求,在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控件(選擇保存文件路徑)。

    程序運行后的界面如下圖所示:







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

    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://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>