Form1.cs
上传用户:idontwrong
上传日期:2022-04-03
资源大小:13k
文件大小:3k
源码类别:

TreeView控件

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. namespace FileNameManager
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void Form1_Load(object sender, EventArgs e)
  18.         {
  19.             foreach(DriveInfo di in DriveInfo.GetDrives())
  20.             {
  21.                 TreeNode tn = new TreeNode();
  22.                 string nodeText = string.Empty;
  23.                 if (di.IsReady)
  24.                 {
  25.                     nodeText = string.Format(@"{0} ({1}:)", di.Name, di.VolumeLabel);
  26.                 }
  27.                 else
  28.                 {
  29.                     nodeText = string.Format(@"({0}:)", di.Name);
  30.                 }
  31.                 tn.Text = nodeText;
  32.                 tn.Name = di.Name;
  33.                 tvFolders.Nodes.Add(AddChildNodes(tn, false));
  34.             }
  35.         }
  36.         private void tvFolders_AfterExpand(object sender, TreeViewEventArgs e)
  37.         {
  38.             foreach (TreeNode node in e.Node.Nodes)
  39.             {
  40.                 AddChildNodes(node, false);
  41.             }
  42.         }
  43.         private TreeNode AddChildNodes(TreeNode node, bool final)
  44.         {
  45.             if (final)
  46.             {
  47.                 return node;
  48.             }
  49.             DirectoryInfo parentDI = new DirectoryInfo(node.Name);
  50.             if (parentDI.FullName.Length == 3)
  51.             {
  52.                 DriveInfo di = new DriveInfo(parentDI.FullName);
  53.                 if (!di.IsReady)
  54.                 {
  55.                     return node;
  56.                 }
  57.             }
  58.             DirectoryInfo[] arrDI = null;
  59.             try
  60.             {
  61.                 arrDI = parentDI.GetDirectories();
  62.             }
  63.             catch
  64.             {
  65.             }
  66.             if (arrDI != null)
  67.             {
  68.                 foreach (DirectoryInfo di in arrDI)
  69.                 {
  70.                     TreeNode tn = new TreeNode();
  71.                     string nodeText = string.Empty;
  72.                     tn.Text = di.Name;
  73.                     tn.Name = di.FullName;
  74.                     node.Nodes.Add(AddChildNodes(tn, true));
  75.                 }
  76.             }
  77.             return node;
  78.         }
  79.         private void tvFolders_AfterSelect(object sender, TreeViewEventArgs e)
  80.         {
  81.             lvFiles.Items.Clear();
  82.             DirectoryInfo di = new DirectoryInfo(e.Node.Name);
  83.             FileInfo[] arrFI = null;
  84.             try
  85.             {
  86.                 arrFI = di.GetFiles();
  87.             }
  88.             catch
  89.             { }
  90.             if (arrFI == null)
  91.             {
  92.                 return;
  93.             }
  94.             foreach (FileInfo fi in arrFI)
  95.             {
  96.                 lvFiles.Items.Add(fi.Name);
  97.             }
  98.         }
  99.     }
  100. }