C # exercise - file browsing operation

xiaoxiao2021-03-06  19

Using system.drawing; using system.collections; using system.bomponentmodel; using system.data; using system.text; using system.io;

A summary description of Namespace WindowsApplication15 {///

/// Form1. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button btnGo; private System.Windows.Forms.TextBox txtPath; private System.Windows.Forms.TreeView TreeView; /// /// Required designer variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose (4);

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// private () {this.btngo = new system.windows.Forms.Button (); this.txtPath = new system.windows.forms.textbox (); this.treeview = new system. Windows.Forms.TreeView (); this.groupbox1 = new system.windows.forms.groupbox (); this.suspendlayout (); // // btngo // this.btngo.location = new system.drawing.point (248 , 8); this.btngo.name = "btngo"; this.btngo.size = new system.drawing.size (32, 20); this.btngo.TabINDEX = 0; this.btngo.text = "go"; THIS.BTNGO.CLICK = New System.EventHandler (this.btngo_click); // // txtpath // this.txtPath.Location = new system.drawing.point (8, 8); this.txtpath.name = "txtpath "; This.txtpath.size = new system.drawing.size (232, 20); this.txtpath.tabindex = 1; this.txtpath.text =" "; /// TreeView // this.treeView.ImageIndex = -1; this.treeview.location = new system.drawing.point (8, 48); this.treeView.Name = "treec"; this.treeView.selectedimageIndex = -1; thi S.TreeView.Size = new system.drawing.size (272, 216); this.treeview.tabindex = 2; /// Groupbox1 // this.dofbox1.location = new system.drawing.point (8, 32) This.groupbox1.name = "groupbox1"; this.groupbox1.size = new system.drawing.size (272, 8); this.groupBox1.tabindex = 3; this.groupbox1.tabstop = false; /// Form1 // this.autoscalebasesize = new system.drawing.size (5, 13); this.Autoscroll = true; this.clientsize = new system.drawing.size (288, 273); this.Controls.add (this.groupbox1) This.Controls.add (this.treeView);

THISTROLS.ADD (this.txtPath); this.controls.add (this.btngo); this.maximumsize = new system.drawing.size (296, 300); this.minimumsize = new system.drawing.size (296 , 300); 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 ());}

string FileSizeString (long fileSize) {const long OneMB = 1024 * 1024; const long OneGB = OneMB * 1024; string unit; double dispSize; if (fileSize <1024) {unit = "bytes"; dispSize = fileSize;} else if ( Filesize

long AddDirectory (DirectoryInfo dir, TreeNode tNode) {FileSystemInfo [] fSIs = dir.GetFileSystemInfos (); FileSystemInfo fSI; int i; long directorySize = 0; for (i = 0; i

IF (fsi.gettype () == typeof (fileInfo)) {fileInfo finfo = (fileInfo) fsi; string s = finfo.name filesizestring (finfo.Length); Treenode NewTreenode = New Treenode (s); tnode.nodes. Add (Newtreenode); DirectorySize = Finfo.Length;

else if (fSI.GetType () == typeof (DirectoryInfo)) {DirectoryInfo dirInfo = (DirectoryInfo) fSI; TreeNode dirTreeNode = new TreeNode (dirInfo.Name); long dirSize = AddDirectory (dirInfo, dirTreeNode); dirTreeNode.Text = FileSizeString (dirSize); tNode.Nodes.Add (dirTreeNode); directorySize = dirSize;} else MessageBox.Show ( "Ooops");} return directorySize;} private void BuildTreeView (string dirName) {DirectoryInfo dirInfo = new DirectoryInfo (dirName); TreeNode tNode = new TreeNode (dirName); long totalSize = AddDirectory (dirInfo, tNode); tNode.Text = FileSizeString (totalSize); treeView.Nodes.Clear (); treeView.Nodes.Add (tNode);} private void btnGo_Click ( Object sender, system.eventargs e) {buildtreeview (txtpath.text);}}}

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

New Post(0)