C # Implementation of listing all subdirectories and files under the directory program (with ideas)

Source: Internet
Author: User
Tags interface save file tostring visual studio
Program to burn their own data into a lot of CDs, I found it hard to find my own files on which disc I needed, so I wrote a program in Visual Studio.NET to list all the subdirectories and files in the directory to make it easier for me to list all the file information on the CD that I burned.

The main algorithm of this program is recursion, the main function is as follows:

Recursively lists all files and subdirectories under the directory

public void Listfiles (FileSystemInfo fileinfo)

{

if (! FileInfo. Exists) return;

DirectoryInfo dirinfo = FileInfo as DirectoryInfo;

if (Dirinfo = null) return; Not a directory

indent++;//indent plus One

FileSystemInfo [] files = dirinfo. Getfilesysteminfos ();

for (int i=0 I traverses all files, subdirectories in directory)

{

FileInfo file = Files[i] as FileInfo;

if (file!= null)//is a file

{

this.richtextbox1.text+= (Writespace (indent) + "| |" +

File. Name + "\ T" + converttokbyte (file. Length) + "\ r");

}

else//is a directory

{

this.richtextbox1.text+= (Writespace (Indent) + "+" +files[i]. Fullname+ "\ r");

Listfiles (Files[i]); Recursive calls to subdirectories

}

}

indent--;//indent minus One

}

The design interface of the program is shown in the following illustration:


Control has two button controls Btnselect and Btnsave (used to select directories and save files, respectively); a RichTextBox control (displays the results), A FolderBrowserDialog control (select directory) and a SaveFileDialog control (choose Save File Path).

The interface of the program after the operation is shown in the following illustration:







The complete code for the program is as follows: (where I added the red)

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

{

///

Summary description of the Form1.

///

public class Form1:System.Windows.Forms.Form

{

Private System.Windows.Forms.RichTextBox RichTextBox1;

public static int indent; Indent value

Private System.Windows.Forms.Button Btnselect;

Private System.Windows.Forms.Button btnsave;

Private System.Windows.Forms.FolderBrowserDialog FolderBrowserDialog1;

Private System.Windows.Forms.SaveFileDialog SaveFileDialog1;

///

The required designer variable.

///

Private System.ComponentModel.Container components = null;



Public Form1 ()

{

//

Required for Windows Forms Designer support

//

InitializeComponent ();



//

TODO: Add any constructor code after the InitializeComponent call

//

}



///

Clean up all resources that are in use.

///

protected override void Dispose (bool disposing)

{

if (disposing)

{

if (Components!= null)

{

Components. Dispose ();

}

}

Base. Dispose (disposing);

}



Code generated #region the Windows forms Designer

///

Designer supports required methods-do not use the Code editor to modify

The contents of this method.

///

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 = "Select Directory";

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 = "Save File";

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



///

The main entry point for the application.

///

[STAThread]

static void Main ()

{

Application.Run (New Form1 ());

}



Recursively lists all files and subdirectories under the directory

public void Listfiles (FileSystemInfo fileinfo)

{

if (! FileInfo. Exists) return;

DirectoryInfo dirinfo = FileInfo as DirectoryInfo;

if (Dirinfo = null) return; Not a directory

indent++;//indent plus One

FileSystemInfo [] files = dirinfo. Getfilesysteminfos ();

for (int i=0 I traverses all files, subdirectories in directory)

{

FileInfo file = Files[i] as FileInfo;

if (file!= null)//is a file

{

this.richtextbox1.text+= (Writespace (indent) + "| |" +

File. Name + "\ T" + converttokbyte (file. Length) + "\ r");

}

else//is a directory

{

this.richtextbox1.text+= (Writespace (Indent) + "+" +files[i]. Fullname+ "\ r");

Listfiles (Files[i]); Recursive calls to subdirectories

}

}

indent--;//indent minus One

}



Controls the indentation space, N is the number of spaces

public string writespace (int n)

{

String Strspace= "";

for (int i=1;i<=n;i++)

strspace+= "";

return strspace;

}



Show number of file bytes

public string Converttokbyte (Long len)

{

float Val;

NumberFormatInfo mynfi = new NumberFormatInfo ();

Mynfi.numberdecimaldigits=1; Show a Decimal



if (len/1024==0)

Return Len. ToString () + "byte";

if (len/1024/1024==0)

{

Val= (float) len/1024;

Return Val. ToString ("N", Mynfi) + "K-byte";

}

Val= (float) len/1024/1024;

Return Val. ToString ("N", Mynfi) + "M byte";



}



private void Btnselect_click (object sender, System.EventArgs e)

{

indent=0;//Indent Clear Zero

This.richTextBox1.ResetText (); Empty the original text in the text box

Select a directory

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)

{

Save the result file

This.richTextBox1.SaveFile (Savefiledialog1.filename,richtextboxstreamtype.plaintext);



}

}



}

}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.