In. net, you can use the file and directory classes for file and folder operations, or the fileinfo and directoryinfo classes. Folder is a term used only in windows. In the operating system theory, people are more accustomed to using the term directory. Microsoft may try to port. Net to other operating systems one day (in fact many people are working on this project ).CompositionFolder class.
Both the file class and Directory class are static classes. The advantage of using them is that you do not need to initialize objects. If you only perform one operation on a file or folder, you 'd better use static methods of the static class, such as file. Move and file. Delete. If you need to perform multiple operations on a file or folder, you 'd better use the fileinfo and directoryinfo classes. Because file and directory are static classes, you need to check a file or folder, such as authentication, before performing operations on a file or folder. If you use the fileinfo class and directoryinfo class, you only need to perform relevant checks when initializing class objects. That is to say, you only need to perform one check. Therefore, if you need to perform multiple operations on a file or folder, it is best to use the fileinfo class and directoryinfo class.
The following sectionCodeDemonstrate how to obtain Folder Information, including obtaining subfolders and files in folders. The directoryinfo class is used here. Of course, you can also use the directory static class.
Void Displayfolder (){ String Folderfullname = @" C: \ Temp " ; Directoryinfo thefolder = New Directoryinfo (folderfullname ); If (! Thefolder. exists) Throw New Directorynotfoundexception ( " Folder not found: " +Folderfullname ); // List all subfolders in folder Console. writeline ( " Subfolders: " ); Foreach (Directoryinfo subfolder In Thefolder. getdirectories () {console. writeline (subfolder. Name );} // List all files in folder Console. writeline (); console. writeline ( " Files: " ); Foreach (Fileinfo File In Thefolder. getfiles () {console. writeline (file. Name );}}
The following shows how to use the fileinfo class to obtain information about a file, including the file creation date and file size. You can also use the File static class.
Void Displayfileinfo (){ String Folderfullname = @" C: \ Temp " ; String Filename = " New text document.txt " ; String Filefullname = Path. Combine (folderfullname, filename); fileinfo thefile = New Fileinfo (filefullname ); If (! Thefile. exists) Throw New Filenotfoundexception ( " File not found: " + Filefullname); console. writeline ( String . Format ( " Creation Time: {0} " , Thefile. creationtime. tostring (); console. writeline ( String . Format ( " Size: {0} bytes " , Thefile. length. tostring ()));}
The following code uses the file class and fileinfo class to demonstrate how to delete an object.
Void Deletefile1 (){ String Filetobedeleted = @" C: \ temp \ new text ~ Document (32.16.txt " ; If (File. exists (filetobedeleted) {file. Delete (filetobedeleted );}} Void Deletefile2 (){ String Filetobedeleted = @" C: \ temp \ new text ~ Document (32.16.txt " ; Fileinfo File = New Fileinfo (filetobedeleted ); If (File. exists) {file. Delete ();}}
The following code uses the Directory class and directoryinfo class to demonstrate how to delete folders.
Void Deletefolder1 (){ String Foldertobedeleted = @" C: \ temp \ test " ; If (Directory. exists (foldertobedeleted )){ // True is recursive delete: Directory. Delete (foldertobedeleted, True );}} Void Deletefolder2 (){ String Foldertobedeleted = @" C: \ temp \ test " ; Directoryinfo folder = New Directoryinfo (foldertobedeleted ); If (Folder. exists) {folder. Delete ( True );}}
The following code uses the file class and fileinfo class to demonstrate how to move a file.
Void Movefile1 (){ String Filetomove = @" C: \ temp \ new text document.txt " ; String Filenewdestination = @" C: \ temp \ test.txt " ; If (File. exists (filetomove )&&! File. exists (filenewdestination) {file. Move (filetomove, filenewdestination );}} Void Movefile2 (){ String Filetomove = @" C: \ temp \ new text document.txt " ; String Filenewdestination = @" C: \ temp \ test.txt " ; Fileinfo File = New Fileinfo (filetomove ); If (File. exists) {file. moveTo (filenewdestination );}}
The following code uses the Directory class and directoryinfo class to demonstrate how to move folders.
Void Movefolder1 (){ String Foldertomove = @" C: \ temp \ test " ; String Foldernewdestination = @" C: \ temp \ Test2 " ; If (Directory. exists (foldertomove) {directory. Move (foldertomove, foldernewdestination );}} Void Movefolder2 (){ String Foldertomove = @" C: \ temp \ test " ; String Foldernewdestination = @" C: \ temp \ Test2 " ; Directoryinfo folder = New Directoryinfo (foldertomove ); If (Folder. exists) {folder. moveTo (foldernewdestination );}}
The following code uses the file class and fileinfo class to demonstrate how to copy files.
Void Copyfile1 (){ String Sourcefile = @" C: \ temp \ new text document.txt " ; String Destinationfile = @" C: \ temp \ test.txt " ; If (File. exists (sourcefile )){ // True is Overwrite File. Copy (sourcefile, destinationfile, True );}} Void Copyfile2 (){ String Sourcefile = @" C: \ temp \ new text document.txt " ; String Destinationfile =@" C: \ temp \ test.txt " ; Fileinfo File = New Fileinfo (sourcefile ); If (File. exists ){ // True is Overwrite File. copyto (destinationfile, True );}}