C # operations on files and folders include deleting, moving, and copying

Source: Internet
Author: User

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.Copy codeThe 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.Copy codeThe 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 an object.Copy codeThe Code is as follows: 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.Copy codeThe 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.Copy codeThe 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.Copy codeThe 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.Copy codeCode: 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 Article

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.