C # implementation lists all subdirectories and files in the directory (with video)

xiaoxiao2021-03-06  71

Turn your own information into a lot of discs, find that even if you are hard to find the required files, so I will write a list of lists and files in the Visual Studio.NET in Visual Studio. Program to facilitate all file information on the burned data disc.

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

// Recursively list all files and subdirectory in the directory

Public void ListFiles (FileSystemInfo fileInfo)

{

IF (! fileInfo.exists) return;

DirectoryInfo Dirinfo = FileInfo As DirectoryInfo;

IF (Dirinfo == Null) return; // is not a directory

Indent ; // Inditive plus one

Filesysteminfo [] files = Dirinfo.getFileSysteminfos ();

For (int i = 0; i

Traversal directory all files, subdirectories

{

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]); // Recursively call the child directory

}

}

Indent -; // Regeneration

}

The design interface of the program is shown below:

The control has two Button controls BtNselect and btnsave (used to select a directory and save file); a RichTextBox control (display result), a FolderBrowserDialog control (select directory) and a SaveFileDialog control (select Save File Path).

The interface after the program run is shown below:

The full code of the program is as follows: (where red is my own added)

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 summary description.

///

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

{

//

// Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//

}

///

/// Clean all the resources being used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

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

}

#ndregion

///

/// The main entry point for the application.

///

[Stathread]

Static void

Main

()

{

Application.run (New Form1 ());

}

// Recursively list all files and subdirectory in the directory

Public void ListFiles (FileSystemInfo fileInfo)

{

IF (! fileInfo.exists) return;

DirectoryInfo Dirinfo = FileInfo As DirectoryInfo;

IF (Dirinfo == Null) return; // is not a directory

Indent ; // Inditive plus one

Filesysteminfo [] files = Dirinfo.getFileSysteminfos ();

For (int i = 0; i

Traversal directory all files, subdirectories

{

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]); // Recursively call the child directory

}

}

Indent -; // Regeneration

}

// Control the indent space, n is the number of spaces

Public String Writespace (int N)

{

String strspace = "";

For (INT i = 1; i <= n; i )

STRSPACE = ""

Return strspace;

}

/ / Display the 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 () "bytes"; if (len / 1024/1024 == 0)

{

Val = (float) len / 1024;

Return Val.Tostring ("N", MyNFI) "K bytes";

}

Val = (float) LEN / 1024/1024;

Return Val.Tostring ("N", MYNFI) "M bytes";

}

Private void btnselect_click (Object Sender, System.Eventargs E)

{

Indent = 0; // Inditive

this.richtextbox1.resettext (); // Clear the original text in the text box

// Select the 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);

}

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-120948.html

New Post(0)