Traverse the local disk based on the Treeview control, and view the history of the treeview Control

Source: Internet
Author: User

Traverse the local disk based on the Treeview control, and view the history of the treeview Control

I. Preface

The Treeview control is used to traverse local file information. It is usually used with the Datagridview and ImageList. The ImageList control is used to provide small images to the TreeView control. The DatagridView usually displays information about files and folders under the TreeNode node.

:

Ii. Code

Initialization form:

Private void ManagerForm_Load (object sender, EventArgs e) {InitialDataGridView (dgv_Local); // initialize the local dgv InitialTreeView (); // initialize the local tree}

 

Initialize the DataGridView:

Public void InitialDataGridView (DataGridView dgv) {DataGridViewColumn dgv_check = new DataGridViewCheckBoxColumn (); dgv_check.HeaderText = ""; dgv. columns. add (dgv_check); maid (); dgv_name.HeaderText = "name"; dgv. columns. add (dgv_name); dgv_name.ReadOnly = true; DataGridViewColumn dgv_length = new DataGridViewTextBoxColumn (); dgv_length.HeaderText = "size"; dgv. columns. add (dgv_length); dgv_length.ReadOnly = true; DataGridViewColumn dgv_type = new DataGridViewTextBoxColumn (); dgv_type.HeaderText = "type"; dgv. columns. add (dgv_type); dgv_type.ReadOnly = true; DataGridViewColumn dgv_version = new DataGridViewTextBoxColumn (); dgv_version.HeaderText = "version"; dgv. columns. add (dgv_version); dgv_version.ReadOnly = true; dgv. selectionMode = maid mode. fullRowSelect; dgv. allowUserToAddRows = false; dgv. rowHeadersVisible = false; dgv. allowUserToResizeRows = false; dgv. columns [0]. width = (int) (double) (dgv. width) * 0.1); dgv. columns [1]. width = (int) (double) (dgv. width) * 0.45 );}

 

Initialize local TreeView:

Public void InitialTreeView () {TreeNode TV _mycomputer = new TreeNode ("My Computer"); latency = 0; latency = 0; TV _Local.Nodes.Add (TV _mycomputer); DriveInfo [] drives = DriveInfo. getDrives (); string driveName = ""; foreach (DriveInfo drive in drives) {switch (drive. driveType) {case DriveType. fixed: driveName = "Local disk (" + drive. name. substring (0, 2) + ")"; break; case DriveType. removable: driveName = "Removable disk (" + drive. name. substring (0, 2) + ")"; break; case DriveType. CDRom: driveName = "DVD drive (" + drive. name. substring (0, 2) + ")"; break; case DriveType. network: driveName = "Network drive (" + drive. name. substring (0, 2) + ")"; break; default: driveName = "unknown (" + drive. name + ")"; break;} TreeNode tr_cd = new TreeNode (); tr_cd.Text = driveName; tr_cd.ImageIndex = 1; tr_cd.SelectedImageIndex = 1; LoadDirectory (Path. getFullPath (drive. name), tr_cd); // obtain the first-level disk information TV _mycomputer.Nodes.Add (tr_cd );}}

 

LoadDirectory method:

Public void LoadDirectory (string path, TreeNode tNode) {try {// traverse Folder Information string [] directorys = Directory. getDirectories (path); foreach (string item in directorys) {if (File. getAttributes (item) & FileAttributes. hidden )! = FileAttributes. hidden) {TreeNode tn_Dir = new TreeNode (Path. getFileName (item); tn_Dir.ImageIndex = 2; tn_Dir.SelectedImageIndex = 2; tn_Dir.Name = item; tNode. nodes. add (tn_Dir) ;}} if (path. contains ("System Volume Information") {return;} // traverses the file Information string [] files = Directory. getFiles (path); foreach (string item in files) {string eName = Path. getExtension (item); if (File. getAttributes (item) & FileAttributes. Hidden )! = FileAttributes. hidden) {TreeNode tn_file = new TreeNode (Path. getFileNameWithoutExtension (item); tn_file.ImageIndex = 3; tn_file.SelectedImageIndex = 3; tn_file.Name = item; tNode. nodes. add (tn_file) ;}}} catch {}}

 

AfterExpand method: Used to quickly load when you click Node, instead of directly loading the disk information of the entire computer during the form loading, because it is too slow

 private void tv_Local_AfterExpand(object sender, TreeViewEventArgs e)        {            if (e.Node.Level >= 1)            {                foreach (TreeNode tnode in e.Node.Nodes)                {                    tnode.Nodes.Clear();                    if (!Path.HasExtension(tnode.Name))                    {                        LoadDirectory(tnode.Name, tnode);                    }                }            }        }

 

NodeMouseClick: click "Node" to display the Node information.

Private void TV _Local_NodeMouseClick (object sender, TreeNodeMouseClickEventArgs e) {Thread. sleep (100); if (e. node. fullPath = "My Computer") {txt_Local.Text = "My Computer";} else if (e. node. fullPath. contains ("(") & e. node. level <= 1) {txt_Local.Text = e. node. fullPath. split ('\') [1]. split (') [1]. replace (')', '\');} else {txt_Local.Text = e. node. name; this. dgv_Local.Rows.Clear (); if (e. node. level> 1) {Loadallinfo (e. node. name, dgv_Local );}}}

 

Loadallinfo: used to display information in the DataGridview.

Public void Loadallinfo (string path, DataGridView dgv) {if (Directory. exists (path) {try {string [] directorys = Directory. getDirectories (path); // obtain the subdirectory foreach (string item in directorys) {if (File. getAttributes (item) & FileAttributes. hidden )! = FileAttributes. hidden) {int index = dgv. rows. add (); dgv. rows [index]. cells [1]. value = item; dgv. rows [index]. cells [2]. value = CountSize (GetDirectoryLength (item )). toString (); dgv. rows [index]. cells [3]. value = "folder"; dgv. rows [index]. cells [4]. value = "" ;}} if (path. contains ("System Volume Information") {return;} string [] files = Directory. getFiles (path); foreach (string item in files) {string eN Ame = Path. GetExtension (item); if (File. GetAttributes (item) & FileAttributes. Hidden )! = FileAttributes. hidden) {int index = dgv. rows. add (); dgv. rows [index]. cells [1]. value = item; System. IO. fileInfo file = new System. IO. fileInfo (item); dgv. rows [index]. cells [2]. value = CountSize (file. length ). toString (); dgv. rows [index]. cells [3]. value = Path. getExtension (item); if (Path. getExtension (item) = ". dll ") {FileVersionInfo ver = FileVersionInfo. getVersionInfo (item); dgv. rows [index]. cells [4]. value = ver. fileVersion;} else {dgv. rows [index]. cells [4]. value = "" ;}}}catch {}} else if (File. exists (path) {try {string item = path; int index = dgv. rows. add (); dgv. rows [index]. cells [1]. value = item; dgv. rows [index]. cells [2]. value = CountSize (item. length ). toString (); dgv. rows [index]. cells [3]. value = Path. getExtension (item); if (Path. getExtension (item) = ". dll ") {FileVersionInfo ver = FileVersionInfo. getVersionInfo (item); dgv. rows [index]. cells [4]. value = ver. fileVersion;} else {dgv. rows [index]. cells [4]. value = "" ;}} catch {}}}

 

Calculate the file size:

# Region file Size conversion public static string CountSize (long Size) {string m_strSize = ""; long FactSize = 0; FactSize = Size; if (FactSize <1024.00) m_strSize = FactSize. toString ("F2") + "B"; else if (FactSize> = 1024.00 & FactSize <1048576) m_strSize = (FactSize/1024.00 ). toString ("F2") + "K"; else if (FactSize> = 1048576 & FactSize <1073741824) m_strSize = (FactSize/1024.00/1024.00 ). toString ("F2") + "M"; else if (FactSize> = 1073741824) m_strSize = (FactSize/1024.00/1024.00/1024.00 ). toString ("F2") + "G"; return m_strSize;} # endregion # public static long GetDirectoryLength (string path) {if (! Directory. exists (path) {return 0;} long size = 0; DirectoryInfo di = new DirectoryInfo (path); foreach (FileInfo fi in di. getFiles () {size + = fi. length;} DirectoryInfo [] dis = di. getDirectories (); if (dis. length> 0) {for (int I = 0; I <dis. length; I ++) {size + = GetDirectoryLength (dis [I]. fullName) ;}} return size ;}# endregion


 

 

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.