Create a small "Resource Manager" by yourself.
Create a small "Resource Manager" by yourself
Note: TvDirectory is the treeView control and lvDirectory is the listView control.
First, set up the interface:
The left side is the treeView control, and the right side is the listView control. (You only need to set griditerator to True for the gridlines of listView .)
Because I/O Stream is used, do not forget to import the namespace: using System. IO;
We only need to create a file class:
1 public class MyFile 2 {3 // file length 4 public float FileLength {get; set;} 5 // file name 6 public string FileName {get; set ;} 7 // file path 8 public string FilePath {get; set;} 9 // file type 10 public string FileType {get; set;} 11}
Now let's load the drive first, and get the four lines of code easily:
1 // load drive 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}
The next step is step-by-step binding:
1 private void BingInfo (TreeNode node) 2 {3 // bind sub-directory 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 // bind the file 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 // bind listView27 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}
You can also bind the listView to a method and call it in the BingInfo () method:
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 }
Call method:
1 ShowFiles(files);
Then add the following code to the AfterSelect event of the treeView control:
1 private void tvDirectory_AfterSelect(object sender, TreeViewEventArgs e)2 {3 TreeNode node = this.tvDirectory.SelectedNode;4 this.BingInfo(node);5 }
Call Load to view the Resource Directory that you want to view:
1 private void Form1_Load(object sender, EventArgs e)2 {3 LoadRootNode();4 }
Since it is a resource manager, there must be basic functions such as file copy and deletion. Next we will implement the copy and delete functions:
The Browse folder window needs to pop up during replication. The FolderBrowserDialog class is required:
1 // prompt the user to select the folder 2 FolderBrowserDialog fbd = new FolderBrowserDialog (); 3 DialogResult result = fbd. ShowDialog ();
If the correct location is selected during replication, perform the copy operation:
1 if (result = DialogResult. OK) 2 {3 desPath = fbd. selectedPath; 4 desPath + = "\" + lvDirectory. selectedItems [0]. subItems [0]. text; 5 // copy File 6 File. copy (sourcePath, desPath); 7 MessageBox. show ("Copied successfully! "); 8}
The complete copy code is as follows:
1 private void tsmiCopy_Click (object sender, EventArgs e) 2 {3 if (this. lvDirectory. selectedItems. count = 0) 4 {5 return; 6} 7 // you are prompted to select the folder 8 FolderBrowserDialog fbd = new FolderBrowserDialog (); 9 DialogResult result = fbd. showDialog (); 10 11 // source file path 12 string sourcePath = lvDirectory. selectedItems [0]. subItems [3]. text; 13 // path of the target file 14 string desPath = null; 15 // if the target location is correctly selected, perform the copy operation 16 17 if (result = Di AlogResult. OK) 18 {19 desPath = fbd. selectedPath; 20 desPath + = "\" + lvDirectory. selectedItems [0]. subItems [0]. text; 21 // copy File 22 File. copy (sourcePath, desPath); 23 MessageBox. show ("Copied successfully! "); 24} 25}
The next step is to delete and refresh the deletion in time:
1 this.lvDirectory.SelectedItems[0].Remove();
The complete deletion code is as follows:
1 private void tmsiDelete_Click (object sender, EventArgs e) 2 {3 if (this. lvDirectory. selectedItems. count = 0) 4 {5 return; 6} 7 // the object to be deleted 8 string sourcePath = lvDirectory. selectedItems [0]. subItems [3]. text; 9 DialogResult result = MessageBox. show (this, "Are you sure you want to delete it? "," Warning! ", MessageBoxButtons. OKCancel, MessageBoxIcon. Warning); 10 if (result = DialogResult. OK) 11 {12 File. Delete (sourcePath); 13 MessageBox. Show (" deleted successfully! "); 14} 15 // refresh 16 this. lvDirectory. SelectedItems [0]. Remove (); 17}
The above is the creation process of a small resource manager.
Interface after completion:
Here I only write the drive D. You can write another drive letter.
Cross-drive replication method:
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 # path writing problems:
If you write
1 string path="D:\Text.txt";
The program reports an "Unrecognized escape sequence" error. Therefore, C # provides two methods:
1 (type 1) change the path to "D: \ Text.txt"
1 (second type) @ "D: \ Text.txt"