I/O Stream-oriented small resource manager and I/O Resource Manager
I first came into contact with IO streams. I found some interesting things based on the knowledge in books and my own exploration. (The knowledge in books is more or less insufficient to solve our problems! At this time, we should solve it ourselves !!!
Therefore, we should have the spirit of exploration when learning. If we are curious, it is a positive mental attitude that helps us acquire more knowledge. We need to maintain it)
Small Resource Manager
This project is mainly used to load all the folders and files (directory, file) in the computer disk)
At this time, I encountered the first problem: only the specified disk is set as the root node in the book, and the folder in the disk is added to the form TreeView.
(I'm going, if my computer is divided into dozens of disk partitions, then I have to specify all of them? I think Microsoft certainly won't be so two. There must be classes for all disks.
Disk is the meaning of the Disk. I tried it. Result = no such category. I have to come slowly)
Then there is
1 private void GetDirectoryInDisk () 2 {3 DriveInfo [] allDisk = System. IO. driveInfo. getDrives (); 4 foreach (DriveInfo item in allDisk) 5 {6 7 TreeNode treeNode = new TreeNode (); 8 treeNode. text = item. name; 9 treeNode. tag = item; 10 tvAllDirectoryInfo. nodes. add (treeNode); 11 DirectoryInfo directory = item. rootDirectory; 12 // get all the folder information under the drive 13 DirectoryInfo [] allDirectory = directory. getDirectories (); 14 foreach (DirectoryInfo dir in allDirectory) 15 {16 TreeNode subTreeNode = new TreeNode (); 17 subTreeNode. text = dir. name; 18 subTreeNode. tag = dir; 19 treeNode. nodes. add (subTreeNode); 20} 21 22} 23}All disks are root nodes.
As a result, Lili's making such a mistake.
(At that time, I had the urge to go to your sister and found the disk type)
1 // Summary: 2 // define the drive type constants, including CDRom, Fixed, Network, NoRootDirectory, Ram, Removable, and Unknown. 3 [Serializable] 4 [ComVisible (true)] 5 public enum DriveType 6 {7 // Abstract: 8 // The drive type is unknown. 9 Unknown = // 11 // Summary: 12 // This drive has no root directory. 13 NoRootDirectory = // 15 // Summary: 16 // This drive is a removable storage device, such as a floppy drive or a USB flash drive. 17 Removable = 2, 18 // 19 // Summary: 20 // This drive is a fixed disk. 21 Fixed = 3,22 // 23 // Summary: 24 // This drive is a network drive. 25 Network = 4,26 // 27 // Summary: 28 // This drive is a CD device, such as a CD or DVD-ROM. 29 CDRom = 5, 30 // 31 // Summary: 32 // This drive is a RAM disk. 33 Ram = 6, 34}Disk Type
I added a judgment to solve this problem (a school disk is a virtual optical drive, resulting in a problem that the device is not ready)
1 foreach (DriveInfo item in allDisk) 2 {3 if (item. driveType = DriveType. fixed) 4 {5 TreeNode treeNode = new TreeNode (); 6 treeNode. text = item. name; 7 treeNode. tag = item; 8 tvAllDirectoryInfo. nodes. add (treeNode); 9 DirectoryInfo directory = item. rootDirectory; 10 // get all the folder information under the drive 11 DirectoryInfo [] allDirectory = directory. getDirectories (); 12 foreach (DirectoryInfo dir in allDirectory) 13 {14 TreeNode subTreeNode = new TreeNode (); 15 subTreeNode. text = dir. name; 16 subTreeNode. tag = dir; 17 treeNode. nodes. add (subTreeNode); 18} 19} 20}The next step is to copy files or folders. (right-click the menu to add, copy, and delete files)
Folders cannot be moved across disks. If you want to move folders, they can only be moved under the same drive!
1 private void CopyDirectory (string srcdir, string desdir) 2 {3 string folderName = srcdir. substring (srcdir. lastIndexOf ("\") + 1); 4 5 string desfolderdir = desdir + "\" + folderName; 6 7 if (desdir. lastIndexOf ("\") = (desdir. length-1) 8 {9 desfolderdir = desdir + folderName; 10} 11 string [] filenames = Directory. getFileSystemEntries (srcdir); 12 13 foreach (string file in filenames) // traverse all objects Files And Directories 14 {15 if (Directory. exists (file) // first as a directory. If this directory Exists, recursively Copy the files under this directory 16 {17 18 string currentdir = desfolderdir + "\" + file. substring (file. lastIndexOf ("\") + 1); 19 if (! Directory. exists (currentdir) 20 {21 Directory. createDirectory (currentdir); 22} 23 24 CopyDirectory (file, desfolderdir); 25} 26 27 else // otherwise copy the file 28 {29 string srcfileName = file. substring (file. lastIndexOf ("\") + 1); 30 31 srcfileName = desfolderdir + "\" + srcfileName; 32 33 34 if (! Directory. Exists (desfolderdir) 35 {36 Directory. CreateDirectory (desfolderdir); 37} 38 39 40 File. Copy (file, srcfileName); 41} 42} // foreach 43}Copy files or folders
Use recursive operations.
This small project has come to an end here. There are still many things to learn and continue to work hard.