TreeView 和 ListView实现资源管理器。

TreeView 和 ListView实现Windows下面的资源管理器,主要用到的知识点foreach,还有就是System.IO里面的TreeNode,DirectoryInfo,FileInfo类。

 

代码
1 using System;
2  using System.Drawing;
3  using System.Collections;
4 using System.ComponentModel;
5 using System.Windows.Forms;
6 using System.Data;
7 using System.IO;
8 namespace Ch5
9 {
10 public partial class Form3 : Form
11 {
12 public Form3()
13 {
14 InitializeComponent();
15 }
16
17
18
19
20
21 private void Form3_Load(object sender, EventArgs e)
22 {
23 string[] drives = Directory.GetLogicalDrives();//得到本机上的驱动器
24 foreach (string drive in drives)//循环
25 {
26
27 MyNode mn = new MyNode(drive, true);
28 this.treeView1.Nodes.Add(mn);//添加驱动器名到TREEVIEW控件上
29
30 }
31 this.listView1.Columns.Add("名称", this.listView1.Width / 4, HorizontalAlignment.Center);
32 this.listView1.Columns.Add("大小", this.listView1.Width / 4, HorizontalAlignment.Center);
33 this.listView1.Columns.Add("类型", this.listView1.Width / 4, HorizontalAlignment.Center);
34 this.listView1.Columns.Add("修改时间", this.listView1.Width / 4, HorizontalAlignment.Center);
35 this.listView1.View = View.Details;
36
37
38 }
39
40 private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
41 {
42 try
43 {
44 MyNode mn = (MyNode)e.Node;
45 if (mn.isLoadFiles == false && mn.Nodes.Count == 0)
46 {
47 DirectoryInfo di = new DirectoryInfo(mn.MyPath);
48 this.listView1.Items.Clear();
49 foreach (DirectoryInfo d in di.GetDirectories())//如果为文件夹
50 {
51 mn.Nodes.Add(new MyNode(d.FullName, false));
52
53 ListViewItem lvi = this.listView1.Items.Add(d.Name);
54 lvi.SubItems.Add("");
55 lvi.SubItems.Add("文件夹");
56
57 lvi.SubItems.Add(d.LastAccessTime.ToString());
58 lvi.ImageIndex = 1;//设置图标
59 }
60 foreach (FileInfo f in di.GetFiles())//如果为文件
61 {
62
63 ListViewItem lvi = this.listView1.Items.Add(f.Name, 0);//加载文件名及图标
64 lvi.SubItems.Add(f.Length.ToString());
65 lvi.SubItems.Add("文件");
66 lvi.SubItems.Add(f.LastAccessTime.ToString());
67 string pic = f.Name.Substring((f.Name.LastIndexOf(".") + 1));
68 switch (pic)
69 {
70 case "rar": lvi.ImageIndex = 2; break;
71 case "zip": lvi.ImageIndex = 2; break;
72 case "doc": lvi.ImageIndex = 3; break;
73 case "pdf": lvi.ImageIndex = 4; break;
74 case "exe": lvi.ImageIndex = 5; break;
75 case "html":lvi.ImageIndex = 6; break;
76 case "mdb": lvi.ImageIndex = 7; break;
77 case "dll": lvi.ImageIndex = 8; break;
78 case "txt": lvi.ImageIndex = 9; break;
79 }
80
81 }
82 }
83 }
84 catch
85 {
86 }
87
88 }
89 public class MyNode : TreeNode//一个继承TreeNode的类
90 {
91 private string mytext = null;
92 public bool isLoadFiles = false;
93 public MyNode(string text, bool isRoot)
94 {
95 mytext = text;
96 if (isRoot)//这里执行是查找本机驱动器的时候执行
97 {
98 base.Text = text.Substring(0, text.LastIndexOf("\\") - 1);
99 }
100 else//这里执行是查找本机文件夹以及文件的时候执行
101 {
102 base.Text = text.Substring(text.LastIndexOf("\\") + 1);
103 }
104 }
105 public string MyPath
106 {
107 get
108 {
109 return mytext;
110 }
111 }
112 }
113
114 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
115 {
116
117 }
118
119 }
120 }

 

posted @ 2010-07-19 09:55  WIN8新人  阅读(634)  评论(0编辑  收藏  举报