一、前言 Treeview控制項常用於遍歷本地文件信息,通常與Datagridview與ImageList搭配。ImageList控制項用於提供小圖片給TreeView控制項,DatagridView通常顯示TreeNode節點下文件及文件夾的信息。 效果圖: 二、代碼 初始化窗體: 初始化DataGri ...
一、前言
Treeview控制項常用於遍歷本地文件信息,通常與Datagridview與ImageList搭配。ImageList控制項用於提供小圖片給TreeView控制項,DatagridView通常顯示TreeNode節點下文件及文件夾的信息。
效果圖:
二、代碼
初始化窗體:
private void ManagerForm_Load(object sender, EventArgs e) { InitialDataGridView(dgv_Local); //初始化本地dgv InitialTreeView(); //初始化本地tree }
初始化DataGridView:
public void InitialDataGridView(DataGridView dgv) { DataGridViewColumn dgv_check = new DataGridViewCheckBoxColumn(); dgv_check.HeaderText = "選擇"; dgv.Columns.Add(dgv_check); DataGridViewColumn dgv_name = new DataGridViewTextBoxColumn(); dgv_name.HeaderText = "名稱"; dgv.Columns.Add(dgv_name); dgv_name.ReadOnly = true; DataGridViewColumn dgv_length = new DataGridViewTextBoxColumn(); dgv_length.HeaderText = "大小"; dgv.Columns.Add(dgv_length); dgv_length.ReadOnly = true; DataGridViewColumn dgv_type = new DataGridViewTextBoxColumn(); dgv_type.HeaderText = "類型"; dgv.Columns.Add(dgv_type); dgv_type.ReadOnly = true; DataGridViewColumn dgv_version = new DataGridViewTextBoxColumn(); dgv_version.HeaderText = "版本"; dgv.Columns.Add(dgv_version); dgv_version.ReadOnly = true; dgv.SelectionMode = DataGridViewSelectionMode.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); }
初始化本地TreeView:
public void InitialTreeView() { TreeNode tv_mycomputer = new TreeNode("我的電腦"); tv_mycomputer.ImageIndex = 0; tv_mycomputer.SelectedImageIndex = 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 = "本地磁碟(" + drive.Name.Substring(0, 2) + ")"; break; case DriveType.Removable: driveName = "可移動磁碟(" + drive.Name.Substring(0, 2) + ")"; break; case DriveType.CDRom: driveName = "DVD驅動器(" + drive.Name.Substring(0, 2) + ")"; break; case DriveType.Network: driveName = "網路驅動器(" + drive.Name.Substring(0, 2) + ")"; break; default: driveName = "未知(" + 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); //取得第一級磁碟信息 tv_mycomputer.Nodes.Add(tr_cd); } }
LoadDirectory方法:
public void LoadDirectory(string path, TreeNode tNode) { try { //遍歷文件夾信息 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; } //遍歷文件信息 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方法:用於點擊Node時快速載入,而不是在窗體載入時直接載入整個電腦的磁碟信息,因為那樣太慢了
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:點擊Node顯示該節點信息
private void tv_Local_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { Thread.Sleep(100); if(e.Node.FullPath=="我的電腦") { 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:用於DataGridview顯示信息
public void Loadallinfo(string path,DataGridView dgv) { if (Directory.Exists(path)) { try { string[] directorys = Directory.GetDirectories(path); //獲取選中treeview節點的子目錄 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 = "文件夾"; dgv.Rows[index].Cells[4].Value = ""; } } if (path.Contains("System Volume Information")) { return; } string[] files = Directory.GetFiles(path); foreach (string item in files) { string eName = 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 { } } }
計算文件大小:
#region 文件大小換算 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 #region 文件夾大小計算 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