Initial exposure to IO stream, based on the knowledge of books, plus their own groping, found some fun things. (The knowledge in books is more or less, sometimes not enough to solve our problems!) We should solve the problem on our own!!!
So when we learn to have the spirit of exploration, thirst for knowledge is to help us to obtain more information of a positive mental attitude, need to maintain)
Small explorer
The main purpose of this project is to load all the folders and files that exist on the computer disk (directory,file)
At this point I met the first question: The book only sets the specified disk as the root node, adding the folder on the disk in the form TreeView
(I'll go, so if I have dozens of partitions on my computer, I'm not going to assign them all?) I think Microsoft certainly won't be so two, there must be classes about all the disks.
Disk is the meaning of disks, I tried, the result = = no This class, your sister's, I had to slowly.
And then there's the
1 Private voidGetdirectoryindisk ()2 {3driveinfo[] Alldisk =System.IO.DriveInfo.GetDrives ();4 foreach(DriveInfo IteminchAlldisk)5 {6 7TreeNode TreeNode =NewTreeNode ();8Treenode.text =item. Name;9Treenode.tag =item;Ten TvAllDirectoryInfo.Nodes.Add (treeNode); OneDirectoryInfo directory =item. RootDirectory; A //get all the folder information under the drive -directoryinfo[] Alldirectory =directory. GetDirectories (); - foreach(DirectoryInfo dirinchalldirectory) the { -TreeNode Subtreenode =NewTreeNode (); -Subtreenode.text =dir. Name; -Subtreenode.tag =dir; + TreeNode.Nodes.Add (subtreenode); - } + A } at}
all disks are root nodes
It turned out to be such a mistake.
(There was a kind of impulse to go to your sister, calm down to find the type of disk)
1 //Summary:2 //defines drive type constants, including CDRom, Fixed, Network, norootdirectory, Ram, removable, and Unknown. 3 [Serializable]4[ComVisible (true)]5 Public enumDriveType6 {7 //Summary:8 //The drive type is unknown. 9Unknown =0,Ten // One //Summary: A //This drive does not have a root directory. -Norootdirectory =1, - // the //Summary: - //this drive is a removable storage device, such as a floppy disk drive or a USB flash drive. -Removable =2, - // + //Summary: - //This drive is a fixed disk. +Fixed =3, A // at //Summary: - //This drive is a network drive. -Network =4, - // - //Summary: - //This drive is a disc device, such as a CD or DVD-ROM. inCDRom =5, - // to //Summary: + //this drive is a RAM disk. -Ram =6, the}
Disk Type
I added a judgment to solve the problem (a school disk is a virtual CD-ROM that creates a problem with the device not ready)
1 foreach(DriveInfo IteminchAlldisk)2 {3 if(item. DriveType = =drivetype.fixed)4 {5TreeNode TreeNode =NewTreeNode ();6Treenode.text =item. Name;7Treenode.tag =item;8 TvAllDirectoryInfo.Nodes.Add (treeNode);9DirectoryInfo directory =item. RootDirectory;Ten //get all the folder information under the drive Onedirectoryinfo[] Alldirectory =directory. GetDirectories (); A foreach(DirectoryInfo dirinchalldirectory) - { -TreeNode Subtreenode =NewTreeNode (); theSubtreenode.text =dir. Name; -Subtreenode.tag =dir; - TreeNode.Nodes.Add (subtreenode); - } + } -}
ModifyThe next step is the replication of the file or folder. (Right-click menu Add copy and delete)
Folders are not able to move across disks, if you want to move only under the same drive!
1 Private voidCopyDirectory (stringSrcdir,stringDesdir)2 {3 stringFolderName = Srcdir. Substring (Srcdir. LastIndexOf ("\\") +1);4 5 stringDesfolderdir = Desdir +"\\"+FolderName;6 7 if(Desdir. LastIndexOf ("\\") = = (Desdir. Length-1))8 {9Desfolderdir = Desdir +FolderName;Ten } One string[] filenames =directory.getfilesystementries (srcdir); A - foreach(stringFileinchFilenames//traverse all files and directories - { the if(directory.exists (file))//as directory processing, if this directory is present, recursively copy the files under the directory - { - - stringCurrentdir = Desfolderdir +"\\"+ file. Substring (file. LastIndexOf ("\\") +1); + if(!directory.exists (currentdir)) - { + directory.createdirectory (currentdir); A } at - copydirectory (file, desfolderdir); - } - - Else //Otherwise, the direct copy file - { in stringSrcfilename = file. Substring (file. LastIndexOf ("\\") +1); - toSrcfilename = Desfolderdir +"\\"+Srcfilename; + - the if(!directory.exists (desfolderdir)) * { $ directory.createdirectory (desfolderdir);Panax Notoginseng } - the + file.copy (File, srcfilename); A } the}//foreach +}
file or folder replication
Use recursive operations.
This small project here will be over, to learn a lot of things, continue to work hard.
A small resource manager for the primary IO stream