posts - 339,comments - 4,views - 25142

今日学习内容:

编写一个简易的文件管理器,通过本次实验,练习 TreeView、ListView 和

SplitContainer 控件的使用,同时熟悉 C#文件系统的操作方法以及 File 类和 Directory类的使用。 

(1)新建 windows 应用程序。

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

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

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

 

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

(5)在 Form1 的构造函数中添加代码:

public Form1()         

{             

InitializeComponent();             

TreeNode myComputerNode = new TreeNode("我的电脑");             treeView1.Nodes.Add(myComputerNode);             listViewShow(myComputerNode);         

}

(6)编写 ListView 控件的方法 listViewShow(TreeNode dirNode)。

private void listViewShow(TreeNode dirNode)

{

listView1.Clear();

if (dirNode.Parent == null)

{

foreach (string DrvName in Directory.GetLogicalDrives())

{

ListViewItem aItem = new ListViewItem(DrvName);

           listView1.Items.Add(aItem);

}

}

else

{

foreach (string DirName in Directory.GetDirectories((string)dirNode.Tag))

{

ListViewItem aItem = new ListViewItem(DirName);

listView1.Items.Add(aItem);

}

foreach (string fileName in Directory.GetFiles((string) dirNode.Tag))

{

ListViewItem aItem=new ListViewItem(fileName);

listView1.Items.Add(aItem);

}

}

}

(7)编写 listViewShow 的重载方法。

private void listViewShow(string dirName)

{

listView1.Clear();

try

{

foreach (string DirName in Directory.GetDirectories(dirName))

{

ListViewItem aItem = new ListViewItem(DirName);

listView1.Items.Add(aItem);

}

foreach (string fileName in Directory.GetFiles(dirName))

{

ListViewItem aItem = new ListViewItem(fileName);

listView1.Items.Add(aItem);

}

}

catch

{

}

}

(8)编写 DirTreeShow(TreeNode dirNode)方法。

private void DirTreeShow(TreeNode dirNode)

{

try

{

if (dirNode.Nodes.Count == 0)                 

{

if (dirNode.Parent == null)

{

foreach (string DrvName in Directory.GetLogicalDrives())

{

TreeNode  aNode= new TreeNode(DrvName);

aNode.Tag=DrvName;

dirNode.Nodes.Add(aNode);

}

}

else

{

foreach(string DirName in Directory.GetDirectories((string)dirNode.Tag))

{

TreeNode  aNode=new TreeNode(DirName);

aNode.Tag=DirName;

dirNode.Nodes.Add(aNode);

}

}

}

}

Catch

{

}

}

(9)编写 TreeView 控件的 AfterSelect 事件代码。

private void  treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)

{

listViewShow(e.Node);

DirTreeShow(e.Node);

}

(10)编写 ListView 控件的 DoubleClick 事件代码。

private void  listView1_DoubleClick(object sender, EventArgs e)

{

foreach (int listIndex in listView1.SelectedIndices)

{

listViewShow(listView1.Items[listIndex].Text);

}

}

(11)单击启动调试工具。

运行代码:(From1.cs)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
 
using System.Collections.Generic;
 
using System.ComponentModel;
 
using System.Data;
 
using System.Drawing;
 
using System.Linq;
 
using System.Text;
 
using System.Threading.Tasks;
 
using System.Windows.Forms;
 
using System.IO;
 
  
 
namespace file
 
{
 
    public partial class Form1 : Form
 
    {
 
        public Form1()
 
        {
 
            InitializeComponent();
 
            TreeNode myComputerNode = new TreeNode("我的电脑"); treeView1.Nodes.Add(myComputerNode); listViewShow(myComputerNode);
 
        }
 
        private void listViewShow(TreeNode dirNode)
 
        {
 
            listView1.Clear();
 
            if (dirNode.Parent == null)
 
            {
 
                foreach (string DrvName in Directory.GetLogicalDrives())
 
                {
 
                    ListViewItem aItem = new ListViewItem(DrvName);
 
                    listView1.Items.Add(aItem);
 
                }
 
            }
 
            else
 
            {
 
                foreach (string DirName in Directory.GetDirectories((string)dirNode.Tag))
 
                {
 
                    ListViewItem aItem = new ListViewItem(DirName);
 
                    listView1.Items.Add(aItem);
 
                }
 
                foreach (string fileName in Directory.GetFiles((string)dirNode.Tag))
 
                {
 
                    ListViewItem aItem = new ListViewItem(fileName);
 
                    listView1.Items.Add(aItem);
 
                }
 
            }
 
        }
 
        private void listViewShow(string dirName)
 
        {
 
            listView1.Clear();
 
            try
 
            {
 
                foreach (string DirName in Directory.GetDirectories(dirName))
 
                {
 
                    ListViewItem aItem = new ListViewItem(DirName);
 
                    listView1.Items.Add(aItem);
 
                }
 
                foreach (string fileName in Directory.GetFiles(dirName))
 
                {
 
                    ListViewItem aItem = new ListViewItem(fileName);
 
                    listView1.Items.Add(aItem);
 
                }
 
            }
 
            catch
 
            {
 
            }
 
        }
 
       private void DirTreeShow(TreeNode dirNode)
 
             {
 
                 try
 
                        {
 
                          if (dirNode.Nodes.Count == 0)                
 
                                   {
 
                                        if (dirNode.Parent == null)
 
                                          {
 
                                                foreach (string DrvName in Directory.GetLogicalDrives())
 
                                                      {
 
                                                              TreeNode  aNode= new TreeNode(DrvName);
 
                                                              aNode.Tag=DrvName;
 
                                                              dirNode.Nodes.Add(aNode);
 
                                                       }
 
                                           }
 
                                       else
 
                                          {
 
                                            foreach(string DirName in Directory.GetDirectories((string)dirNode.Tag))
 
                                                  {
 
                                                         TreeNode  aNode=new TreeNode(DirName);
 
                                                         aNode.Tag=DirName;
 
                                                         dirNode.Nodes.Add(aNode);
 
                                                   }
 
                                           }
 
                                   }
 
                        }
 
                               catch
 
                                     {
 
                                      }
 
         }
 
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
 
        {
 
            foreach (int listIndex in listView1.SelectedIndices)
 
            {
 
                listViewShow(listView1.Items[listIndex].Text);
 
            }
 
        }
 
  
 
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
 
        {
 
            listViewShow(e.Node);
 
            DirTreeShow(e.Node);
 
        }
 
    }
 
}

 运行截图:

 

 

posted on   白日梦想家~  阅读(303)  评论(0编辑  收藏  举报
< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示