自己動手做一個小型“資源管理器”吧


自己動手做一個小型“資源管理器”吧

 

:tvDirectory是treeView控件,lvDirectory是listView控件

首先搭建一下界面:

左邊是treeView控件,右邊是listView控件。(listView的網格線只需把GridLins設置成True就可以了。

由於要用到IO流,所以別忘了導入命名空間:using System.IO;

 

我們只要創建一個文件類就可以了:

 1 public class MyFile
 2     {
 3         //文件長度
 4         public float FileLength { get; set; }
 5         //文件名
 6         public string FileName { get; set; }
 7         //文件路徑
 8         public string FilePath { get; set; }
 9         //文件類型
10         public string FileType { get; set; }
11     }

 

現在先來加載一下驅動器,四行代碼輕松搞定:

1 //加載驅動器
2         private void LoadRootNode()
3         {
4             TreeNode tn = new TreeNode();
5             tn.Text = "D:\\";
6             tn.Tag = "D:\\";
7             this.tvDirectory.Nodes.Add(tn);
8         }

 

接下來是一步步的綁定:

 1 private void BingInfo(TreeNode node) 
 2         {
 3             //綁定子目錄
 4             DirectoryInfo directoryInfo = new DirectoryInfo(node.Tag.ToString());
 5             DirectoryInfo[] dirs = directoryInfo.GetDirectories();
 6             foreach (DirectoryInfo di in dirs)
 7             {
 8                 TreeNode temp = new TreeNode();
 9                 temp.Text = di.Name;
10                 temp.Tag = di.FullName;
11                 node.Nodes.Add(temp);
12             }
13 
14             //綁定本目錄中的文件
15             FileInfo[] fileInfo = directoryInfo.GetFiles();
16             List<MyFile> files = new List<MyFile>();
17             foreach (FileInfo myFile in fileInfo)
18             {
19                 MyFile file = new MyFile();
20                 file.FileName = myFile.Name;
21                 file.FileLength = myFile.Length;
22                 file.FileType = myFile.Extension;
23                 file.FilePath = myFile.FullName;
24                 files.Add(file);
25             }
26             //綁定listView
27             ListViewItem item = null;
28             this.lvDirectory.Items.Clear();
29             foreach (MyFile file in files)
30             {
31                 item = new ListViewItem();
32                 item.Text = file.FileName;
33                 item.SubItems.Add(file.FileLength.ToString());
34                 item.SubItems.Add(file.FileType);
35                 item.SubItems.Add(file.FilePath);
36                 this.lvDirectory.Items.Add(item);
37             }

 

綁定listView也可以單獨寫成一個方法然后在BingInfo()方法中調用一下:

 1 public void ShowFiles(List<MyFile> files)
 2         {
 3             ListViewItem item = null;
 4             this.lvDirectory.Items.Clear();
 5             foreach (MyFile file in files)
 6             {
 7                 item = new ListViewItem();
 8                 item.Text = file.FileName;
 9                 item.SubItems.Add(file.FileLength.ToString());
10                 item.SubItems.Add(file.FileType);
11                 item.SubItems.Add(file.FilePath);
12                 this.lvDirectory.Items.Add(item);
13             }
14 
15         }

 

調用方法:

1 ShowFiles(files);

 

之后需要在treeView控件的AfterSelect事件中添加如下代碼:

1         private void tvDirectory_AfterSelect(object sender, TreeViewEventArgs e)
2         {
3             TreeNode node = this.tvDirectory.SelectedNode;
4             this.BingInfo(node);
5         }

 

再在Load中調用一下就可以正常查看顯示你所想要看的資源目錄了:

1 private void Form1_Load(object sender, EventArgs e)
2         {
3             LoadRootNode();
4         }

 

既然是資源管理器,那么肯定要有文件的復制和刪除等基本功能,接下來實現復制刪除功能:

復制時需要彈出“瀏覽文件夾”窗口,這時需要用到FolderBrowserDialog類:

1             //提示用戶選擇文件夾
2             FolderBrowserDialog fbd = new FolderBrowserDialog();
3             DialogResult result = fbd.ShowDialog();

 

復制時如果選擇了正確的位置,則執行復制操作:

1             if (result == DialogResult.OK)
2             {
3                 desPath = fbd.SelectedPath;
4                 desPath += "\\" + lvDirectory.SelectedItems[0].SubItems[0].Text;
5                 //復制文件
6                 File.Copy(sourcePath, desPath);
7                 MessageBox.Show("復制成功!");
8             }   

 

完整復制代碼如下:

 1 private void tsmiCopy_Click(object sender, EventArgs e)
 2         {
 3             if (this.lvDirectory.SelectedItems.Count == 0)
 4             {
 5                 return;
 6             }
 7             //提示用戶選擇文件夾
 8             FolderBrowserDialog fbd = new FolderBrowserDialog();
 9             DialogResult result = fbd.ShowDialog();
10 
11             //源文件路徑
12             string sourcePath = lvDirectory.SelectedItems[0].SubItems[3].Text;
13             //目標文件路徑
14             string desPath = null;
15             //如果正確選擇目標位置,執行復制操作
16 
17             if (result == DialogResult.OK)
18             {
19                 desPath = fbd.SelectedPath;
20                 desPath += "\\" + lvDirectory.SelectedItems[0].SubItems[0].Text;
21                 //復制文件
22                 File.Copy(sourcePath, desPath);
23                 MessageBox.Show("復制成功!");
24             }   
25         }

 

接下來是刪除,刪除完要及時刷新:

1 this.lvDirectory.SelectedItems[0].Remove();

 

完整刪除代碼如下:

 1 private void tmsiDelete_Click(object sender, EventArgs e)
 2         {
 3             if (this.lvDirectory.SelectedItems.Count == 0)
 4             {
 5                 return;
 6             }
 7             //要刪除的文件
 8             string sourcePath = lvDirectory.SelectedItems[0].SubItems[3].Text;
 9             DialogResult result = MessageBox.Show(this, "確定要刪除嗎?", "警告!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
10             if (result == DialogResult.OK)
11             {
12                 File.Delete(sourcePath);
13                 MessageBox.Show("刪除成功!");
14             }
15             //刷新一下
16             this.lvDirectory.SelectedItems[0].Remove();
17         }

以上就是一個小型的資源管理器的制作過程了。

完成之后界面:

這里我只寫了D盤,大家可以接着寫其他的盤符。

 

跨驅動器復制方法:

 1 private void CopyDirectoryAndFiles(string des, DirectoryInfo srcDir)
 2         {
 3             if (!des.EndsWith("\\"))
 4             {
 5                 des += "\\";
 6             }
 7             string desPath = des + srcDir.Name + "\\";
 8             if(!Directory.Exists(desPath))
 9             {
10                 Directory.CreateDirectory(desPath);
11             }
12              
13             foreach (FileInfo file in srcDir.GetFiles())
14             {
15                 file.CopyTo(desPath + file.Name, true);
16             }
17             foreach (DirectoryInfo dirinfo in srcDir.GetDirectories())
18             {
19                 CopyDirectoryAndFiles(desPath, dirinfo);
20             }
21         }

 

 

C#路徑書寫的問題:

如果寫成

1 string path="D:\Text.txt";

 

程序會報“無法識別的轉義序列”錯誤,所以C#提供了兩種方法:

1 (第一種)將路徑改為“D:\\Text.txt”
1 (第二種)@“D:\\Text.txt”

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM