返回顶部

一缕半夏微光

温柔半两,从容一生

导航

简易文件管理器(C#)

效果如下:

将窗体 Form 的 Text 属性设置为“简易的文件管理器”。 

窗体上添加 SplitContainer、TreeView、ListView 控件,设置属性。将 TreeView 控

件的 Dock 属性设置为 Left,ListView 控件的 Dock 属性设置为 Fill,MultiSelect 属性设置为False,View 属性设置为 List。 

Form1.cs 中,添加对命名空间 using System.IO 的引用。

treeView放置在左边,ListView放置在右边

Form1.cs代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.IO;
 11 
 12 namespace Net_4
 13 {
 14     public partial class Form1 : Form
 15     {
 16         public Form1()
 17         {
 18             InitializeComponent();
 19             TreeNode myComputerNode = new TreeNode("我的电脑");
 20             treeView1.Nodes.Add(myComputerNode);
 21             listViewShow(myComputerNode);
 22         }
 23 
 24         //ListView 控件的方法 listViewShow(TreeNode dirNode)
 25         private void listViewShow(TreeNode dirNode)
 26         {
 27             listView1.Clear();
 28             if (dirNode.Parent == null)
 29             {
 30                 foreach (string DrvName in Directory.GetLogicalDrives())
 31                 {
 32                     ListViewItem aItem = new ListViewItem(DrvName);
 33                     listView1.Items.Add(aItem);
 34                 }
 35             }
 36             else
 37             {
 38                 foreach (string DirName in Directory.GetDirectories((string)dirNode.Tag))
 39                 {
 40                     ListViewItem aItem = new ListViewItem(DirName);
 41                     listView1.Items.Add(aItem);
 42                 }
 43 
 44                 foreach (string fileName in Directory.GetFiles((string)dirNode.Tag))
 45                 {
 46                     ListViewItem aItem = new ListViewItem(fileName);
 47                     listView1.Items.Add(aItem);
 48                 }
 49             }
 50         }
 51 
 52         //listViewShow 的重载方法
 53         private void listViewShow(string dirName)
 54         {
 55             listView1.Clear();
 56             try
 57             {
 58                 foreach (string DirName in Directory.GetDirectories(dirName))
 59                 {
 60                     ListViewItem aItem = new ListViewItem(DirName);
 61                     listView1.Items.Add(aItem);
 62                 }
 63                 foreach (string fileName in Directory.GetFiles(dirName))
 64                 {
 65                     ListViewItem aItem = new ListViewItem(fileName);
 66                     listView1.Items.Add(aItem);
 67                 }
 68             }
 69             catch
 70             {
 71             }
 72         }
 73 
 74         private void DirTreeShow(TreeNode dirNode)
 75         {
 76             try
 77             {
 78                 if (dirNode.Nodes.Count == 0)
 79                 {
 80                     if (dirNode.Parent == null)
 81                     {
 82                         foreach (string DrvName in Directory.GetLogicalDrives())
 83                         {
 84                             TreeNode aNode = new TreeNode(DrvName);
 85                             aNode.Tag = DrvName;
 86                             dirNode.Nodes.Add(aNode);
 87                         }
 88                     }
 89                     else
 90                     {
 91                         foreach (string DirName in Directory.GetDirectories((string)dirNode.Tag))
 92                         {
 93                             TreeNode aNode = new TreeNode(DirName);
 94                             aNode.Tag = DirName;
 95                             dirNode.Nodes.Add(aNode);
 96                         }
 97                     }
 98                 }
 99             }
100             catch
101             {
102             }
103         }
104 
105         private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
106         {
107 
108         }
109 
110         private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
111         {
112 
113         }
114 
115         private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
116         {
117             listViewShow(e.Node);
118             DirTreeShow(e.Node);
119         }
120 
121         private void listView1_DoubleClick(object sender, EventArgs e)
122         {
123             foreach (int listIndex in listView1.SelectedIndices)
124             {
125                 listViewShow(listView1.Items[listIndex].Text);
126             }
127         } 
128     }
129 }

 

posted on 2021-11-17 21:32  一缕半夏微光  阅读(336)  评论(1编辑  收藏  举报