Form1.cs
上传用户:idontwrong
上传日期:2022-04-03
资源大小:13k
文件大小:3k
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- namespace FileNameManager
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- foreach(DriveInfo di in DriveInfo.GetDrives())
- {
- TreeNode tn = new TreeNode();
- string nodeText = string.Empty;
- if (di.IsReady)
- {
- nodeText = string.Format(@"{0} ({1}:)", di.Name, di.VolumeLabel);
- }
- else
- {
- nodeText = string.Format(@"({0}:)", di.Name);
- }
- tn.Text = nodeText;
- tn.Name = di.Name;
- tvFolders.Nodes.Add(AddChildNodes(tn, false));
- }
- }
- private void tvFolders_AfterExpand(object sender, TreeViewEventArgs e)
- {
- foreach (TreeNode node in e.Node.Nodes)
- {
- AddChildNodes(node, false);
- }
- }
- private TreeNode AddChildNodes(TreeNode node, bool final)
- {
- if (final)
- {
- return node;
- }
- DirectoryInfo parentDI = new DirectoryInfo(node.Name);
- if (parentDI.FullName.Length == 3)
- {
- DriveInfo di = new DriveInfo(parentDI.FullName);
- if (!di.IsReady)
- {
- return node;
- }
- }
- DirectoryInfo[] arrDI = null;
- try
- {
- arrDI = parentDI.GetDirectories();
- }
- catch
- {
- }
- if (arrDI != null)
- {
- foreach (DirectoryInfo di in arrDI)
- {
- TreeNode tn = new TreeNode();
- string nodeText = string.Empty;
- tn.Text = di.Name;
- tn.Name = di.FullName;
- node.Nodes.Add(AddChildNodes(tn, true));
- }
- }
- return node;
- }
- private void tvFolders_AfterSelect(object sender, TreeViewEventArgs e)
- {
- lvFiles.Items.Clear();
- DirectoryInfo di = new DirectoryInfo(e.Node.Name);
- FileInfo[] arrFI = null;
- try
- {
- arrFI = di.GetFiles();
- }
- catch
- { }
- if (arrFI == null)
- {
- return;
- }
- foreach (FileInfo fi in arrFI)
- {
- lvFiles.Items.Add(fi.Name);
- }
- }
- }
- }