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

xiaoxiao2021-03-05  30

The main algorithm of this program is recursive, the main function is as follows: // Recutorizes all files and subdirectories in the directory PUBLIC VOID LISTFO Fileinfo {if (! FileInfo.exists) Return; DirectoryInfo Dirinfo = fileInfo as DirectoryInfo; IF Dirinfo == NULL) RETURN; // Not a directory indent ; // indentation plus a filesisysteminfo [] files = Dirinfo.getFileSysteminfos (); for (int i = 0; i )> Traversal Directory All files, subdirectory {FileInfo file = files [i] as fileinfo; if (file! = Null) // is the file {this.richtextbox1.text = (indent) "-" file.name "/ t" ConvertTokbyte (file.length) "} else // is the directory {this.richtextbox1.text = (WRITESPACE (Indent) " " Files [i] .fullname " / r "); ListFiles [I]); / / The subdirectory is recursively called}} indent -; // Read into the design interface of the program, as shown below: The control has two Button controls BTNSELECT and BTNSAVE (used to select the directory, etc. And save files); 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 as shown below: The full code of the program is as follows: (where red is my own added) Using system; use system.drawing; use system.collections; using system.componentmodel; using system.windows.Forms Using system.data; using system.globalization; usingspace listfile_windows {///// //form1 summary description.

/// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.RichTextBox richTextBox1; public static int indent; // 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; //// The designer variable. ///Prate system.componentmodel.container components = null; public form1 () {// // Windows Form Designer Support for // InitializationComponent (); // // Todo: Add any construct after INITIALIZECOMPONENT call Function code //} / / / / / Clean all the resources being used. /// Protected {if (disponents! = Null) {components.dispose ();}} Base.dispose ();} #Region Windows Form Designer Generated Code // /// Designer Supports the required method - Do not modify the // this method using the code editor.

/// 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); this.richtextbox1.name = "richtextbox1"; this.richtextbox1.size = new system.drawing.size (528, 400); this.richtextbox1.tabindex = 0 "This.richtextbox1.text =" "; // // btnselect // this.btnselect///////-sLocation = 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.btnet "; this.controls.add (this.richtextbox1); this.name =" Form1 "

This.Text = "form1"; this.ResumeLayout (false);} #ENDREGION / / / // The primary entry point of the application.

/// [stathread] static void main () {Application.run (new form1 ());} // Remove all files and subdirectories in 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 < i )> Traversal directory all files, subdirectory {fileinfo file = files [i] as fileinfo; if (file! = null) // is file {this.richtextBox1.text = (indent) "| File.Name "/ T" ConvertTokbyte (file.length) "} else // is the directory {this.richtextbox1.text = (indent) " " Files [i] .Fullname "/ r"); listfiles (files [i]); / / pair subdirectory to recursively call}} indent -; // Confincondens 1} // Control indent space, N is a space number PUBLIC STRING WRITESPACE (INT N) {String Strspace = ""; for (INT i = 1; i <= n; i ) s Trspace = "";} // Display file byte number public string converTtokbyte (long LEN) {float val; Numberformatinfo mynfi = new numberFormatinfo (); mynfi.numberdecimaldigits = 1; // Display 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 byte";} val = (float) LEN / 1024/1024; RETURN VAL.TOSTRING ("N"

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

New Post(0)