C # operations on files and folders include deleting, moving, and copying,
In. for File and Folder operations, you can use the File and Directory classes, or the FileInfo and DirectoryInfo classes, for more information, see.
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), so it still uses Directory to name folder classes.
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 code shows how to obtain the folder information, including the subfolders and files in the folder. The DirectoryInfo class is used here. Of course, you can also use the Directory static class.
The Code is as follows:
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.
The Code is as follows:
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 files.
The Code is as follows:
void DeleteFile1() { string fileToBeDeleted = @"c:\temp\New Text~ Document (3).txt"; if (File.Exists(fileToBeDeleted)) { File.Delete(fileToBeDeleted); } } void DeleteFile2() { string fileToBeDeleted = @"c:\temp\New Text~ Document (3).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.
The Code is as follows:
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.
The Code is as follows:
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.
The Code is as follows:
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.
The Code is as follows:
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); } }