C#小型资源管理器

搭建窗体

左侧为TreeView   代码中tvDirectory为TreeView

右侧为ListView   代码中lvDirectory为ListView  设置网格线为GridLines属性为True

还有一个右键菜单contextMenuStrip,在ListView中绑定

 

接下来创建一个类,存储文件信息

1          //文件长度
2          public float FileLength { get; set; }
3          //文件名
4          public string FileName { get; set; }
5          //文件路径
6          public string FilePath { get; set; }
7          //文件类型
8          public string FileType { get; set; }    

 

在窗体类中写一个方法用来加载驱动器

看你电脑的具体盘符了

 1         #region 加载驱动器
 2 
 3         private void LoadRootNode()
 4         {
 5             TreeNode tn = new TreeNode();
 6             tn.Text = "C:\\";
 7             tn.Tag = "C:\\";
 8             this.tvDirectory.Nodes.Add(tn);
 9 
10 
11             TreeNode tn1 = new TreeNode();
12             tn1.Text = "D:\\";
13             tn1.Tag = "D:\\";
14             this.tvDirectory.Nodes.Add(tn1);
15 
16             TreeNode tn2 = new TreeNode();
17             tn2.Text = "F:\\";
18             tn2.Tag = "F:\\";
19             this.tvDirectory.Nodes.Add(tn2);
20 
21             TreeNode tn3 = new TreeNode();
22             tn3.Text = "G:\\";
23             tn3.Tag = "G:\\";
24             this.tvDirectory.Nodes.Add(tn3);
25 
26             TreeNode tn4 = new TreeNode();
27             tn4.Text = "H:\\";
28             tn4.Tag = "H:\\";
29             this.tvDirectory.Nodes.Add(tn4);
30         }
31         #endregion

 

在窗体类中创建一个方法,用来写绑定的方法

1         private void BingInfo(TreeNode node)
2         {
3         }

在该方法中书写如下内容

1.绑定子目录

1             DirectoryInfo directoryInfo = new DirectoryInfo(node.Tag.ToString());
2             DirectoryInfo[] dirs = directoryInfo.GetDirectories();
3             foreach (DirectoryInfo di in dirs)
4             {
5                 TreeNode temp = new TreeNode();
6                 temp.Text = di.Name;
7                 temp.Tag = di.FullName;
8                 node.Nodes.Add(temp);
9             }     

 

2.绑定本目录中的文件

 1             FileInfo[] fileInfo = directoryInfo.GetFiles();
 2             List<MyFile> files = new List<MyFile>();
 3             foreach (FileInfo myFile in fileInfo)
 4             {
 5                 MyFile file = new MyFile();
 6                 file.FileName = myFile.Name;
 7                 file.FileLength = myFile.Length;
 8                 file.FileType = myFile.Extension;
 9                 file.FilePath = myFile.FullName;
10                 files.Add(file);
11             } 

3.绑定ListView

 1             ListViewItem item = null;
 2             this.lvDirectory.Items.Clear();
 3             foreach (MyFile file in files)
 4             {
 5                 item = new ListViewItem();
 6                 item.Text = file.FileName;
 7                 item.SubItems.Add(file.FileLength.ToString());
 8                 item.SubItems.Add(file.FileType);
 9                 item.SubItems.Add(file.FilePath);
10                 this.lvDirectory.Items.Add(item);
11             } 

 

在Tree View中的AfterSelect事件中写

1             TreeNode node = this.tvDirectory.SelectedNode;
2             this.BingInfo(node);

 

在右键菜单的复制点击事件中写

 1             if (this.lvDirectory.SelectedItems.Count == 0)
 2             {
 3                 return;
 4             }
 5             //提示用户选择文件夹
 6             FolderBrowserDialog fbd = new FolderBrowserDialog();
 7             DialogResult result = fbd.ShowDialog();
 8 
 9             //源文件路径
10             string sourcePath = lvDirectory.SelectedItems[0].SubItems[3].Text;
11             //目标文件路径
12             string desPath = null;
13             //如果正确选择目标位置,执行复制操作
14 
15             if (result == DialogResult.OK)
16             {
17                 desPath = fbd.SelectedPath;
18                 desPath += "\\" + lvDirectory.SelectedItems[0].SubItems[0].Text;
19                 //复制文件
20                 File.Copy(sourcePath, desPath);
21                 MessageBox.Show("复制成功!");
22             }

 

在右键菜单的删除点击事件中写

 1             if (this.lvDirectory.SelectedItems.Count == 0)
 2             {
 3                 return;
 4             }
 5             //要删除的文件
 6             string sourcePath = lvDirectory.SelectedItems[0].SubItems[3].Text;
 7             DialogResult result = MessageBox.Show(this, "确定要删除吗?", "警告!",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
 8              if (result == DialogResult.OK)
 9              {
10                  File.Delete(sourcePath);
11                  MessageBox.Show("删除成功!");
12              }
13              //移除
14              this.lvDirectory.SelectedItems[0].Remove();

最后在窗体Load事件中写

   LoadRootNode();

 

posted @ 2018-04-20 16:26  之。  阅读(185)  评论(1编辑  收藏  举报