C's operations on files and folders include deleting moving and copying

  • 2020-05-07 20:18:11
  • OfStack

In.Net, operations on files (File) and folders (Folder) can use the File and Directory classes, or the FileInfo and DirectoryInfo classes. Folder (Folder) is a noun used only in the Windows operating system. In operating system theory, people are more used to the term directory (Directory). Perhaps Microsoft decided to port.Net to other operating systems on the 1st (and there are actually a lot of people working on this as well), so they named the actions folder class after Directory.

Both the File and Directory classes are static classes. The advantage of using them is that there is no need to initialize the object. If you only do one operation on a file or folder, you'd better use the static methods of the static class, such as File.Move, File.Delete, etc. If you need to do multiple operations on a file or folder, it is best to use the FileInfo and DirectoryInfo classes. Because File and Directory are static classes, they need to check every time you work on a file or folder, such as authentication. If you use the FileInfo class and DirectoryInfo class, you only need to do the relevant checking when initializing the object of the class, that is, you only need to do it once, so if you need to operate a file or folder several times, it is better to use the FileInfo class and DirectoryInfo class.

The following code demonstrates how to get information about a folder, including the subfolders under the folder, and the files under the folder. This is done using the DirectoryInfo class, but 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 get information about a file, including the date the file was created, the size of the file, and so on. Of course you can also do this using 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 respectively to demonstrate how to delete a file
 
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 respectively to demonstrate how to delete a folder
 
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 respectively 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 and DirectoryInfo classes to demonstrate how to move folders, respectively
 
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 respectively 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); 
} 
} 

Related articles: