Small explorer, IO Operations, common operations for reading and writing files and directories

Source: Internet
Author: User

Solution:

Small summary:

With IO stream, the File,directoryinfo method binds to the TreeView control and Deletes, reads, and can master the IO stream

Main Page:

1    Private voidForm1_Load (Objectsender, EventArgs e)2         {3             4             //Create a root node5TreeNode root =NewTreeNode ();6Root. Text ="d:\\";//the folder name is displayed (individual name)7Root. Tag ="d:\\";//absolute path to save folder8 9 tvExplore.Nodes.Add (root);Ten  One  A             //Load all sub-folders under D disk -  -             //01. Load all the files under the D disk on the left TreeView control the             //02. Display only folders in the TreeView -             //03. The file of the selected node is displayed in the right ListView -  -         } +  -         Private stringpath; +         PrivateDirectoryInfo info; A  at          Public voidloaddirectory (TreeNode selectnode) -         { - selectNode.Nodes.Clear (); -             //02. Load all folders under this path (SelectNode.Tag.ToString ()) -Path =selectNode.Tag.ToString (); -info =NewDirectoryInfo (path); indirectoryinfo[] Directorys =info. GetDirectories (); -             foreach(DirectoryInfo DirectoryInfoinchDirectorys) to             { +                 //creates a node on the TreeView for each traversal of a DirectoryInfo object -TreeNode tn =NewTreeNode (); theTn. Text =Directoryinfo.name; *Tn. Tag =Directoryinfo.fullname; $ Panax Notoginseng SelectNode.Nodes.Add (TN); -             } the         } + 
 A         //Load All Files the          Public voidloadallfiles () +         { - lvFiles.Items.Clear (); $             //03. Load all files under the currently selected node $fileinfo[] Files =info. GetFiles (); -             foreach(FileInfo fileinchfiles) -             { the                 //each traversal to a file object, forming a record on the ListView -ListViewItem Lvitem =NewListViewItem (file. Name);WuyiLvitem. SubItems.Add (file. length*1.0/1024x768/1024x768+"MB");//File Size the                 //file type (extension) - Lvitem. SubItems.Add (file. Extension); Wu                 //file path - Lvitem. SubItems.Add (file. FullName); About LvFiles.Items.Add (lvitem); $             } -         } -  -         Private voidTvexplore_afterselect (Objectsender, TreeViewEventArgs e) A         { +             //01. Get the selected Node object theTreeNode Selectnode =Tvexplore.selectednode; -             //02. Load the current node's folder $ loaddirectory (selectnode); the loadallfiles (); the         } the  the         Private voidLvfiles_doubleclick (Objectsender, EventArgs e) -         { in             //gets the path column for the selected file the             stringPath = lvfiles.selecteditems[0]. subitems[3]. Text; the Process.Start (path); About         } the  the         Private voidButton1_Click (Objectsender, EventArgs e) the         { +              -         } the Bayi         Private voidTisicopy_click (Objectsender, EventArgs e) the         { the             //gets the node in the TV selected by the user -TreeNode selectnode=Tvexplore.selectednode; -             stringPath=selectNode.Tag.ToString (); theDialogResult dr=Folderbrowserdialog1.showdialog (); the             if(dr==DialogResult.OK) the             { the copydirectory (path,folderbrowserdialog1.selectedpath); -             } the         }
the //Click the tree structure to copy the folder the /// <summary>94 /// the /// </summary> the /// <param name= "Srcdir" >Source Path</param> the /// <param name= "Desdir" >Target Path</param>98 Private voidCopyDirectory (stringSrcdir,stringDesdir) About { - //d:\\ Beauty Chart101 stringFolderName = Srcdir. Substring (Srcdir. LastIndexOf ("\\") +1);102 103 stringDesfolderdir = Desdir +"\\"+FolderName;104 the if(Desdir. LastIndexOf ("\\") = = (Desdir. Length-1))106 {107Desfolderdir = Desdir +FolderName;108 }109 string[] filenames =directory.getfilesystementries (srcdir); the 111 foreach(stringFileinchFilenames//traverse all files and directories the {113 if(directory.exists (file))//as directory processing, if this directory is present, recursively copy the files under the directory the { the the stringCurrentdir = Desfolderdir +"\\"+ file. Substring (file. LastIndexOf ("\\") +1);117 if(!directory.exists (currentdir))118 {119 directory.createdirectory (currentdir); - }121 122 copydirectory (file, desfolderdir);123 }124 the Else //Otherwise, the direct copy file126 {127 stringSrcfilename = file. Substring (file. LastIndexOf ("\\") +1); - 129Srcfilename = Desfolderdir +"\\"+Srcfilename; the 131 the if(!directory.exists (desfolderdir))133 {134 directory.createdirectory (desfolderdir);135 }136 137 138 file.copy (File, srcfilename);139 } $}//foreach141}

To be adjourned ......... ..................

Small explorer, IO Operations, common operations for reading and writing files and directories

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.