C # Operations on files and folders include Delete, move, and copy _c# tutorial

Source: Internet
Author: User
Tags static class
In. NET, you can use the file class and the directory class for actions on files and folders (folder), or you can use the FileInfo class and the DirectoryInfo class. Folders are nouns that are used only in Windows operating systems. In the theory of operating systems, people are more accustomed to using the term directory. Perhaps Microsoft will someday. NET porting to other operating systems (in fact, many people are also doing this project), so it's also a directory to name the Operations folder class.

Both the file class and the directory class are static classes. The advantage of using them is that you do not need to initialize the object. If you are only doing one operation on a file or folder, you'd better use the static class static method, such as File.move,file.delete and so on. If you need to do multiple operations on a file or folder, it's best to use the FileInfo and DirectoryInfo classes. Because the file class and directory are static classes, each time you work on a file or folder, they need to do some checking on the file or folder, such as authentication. If you use the FileInfo class and the DirectoryInfo class, you only need to do a check when you initialize the object of the class, which means you only have to do it once, so if you need to do multiple operations on a file or folder, It is best to use the FileInfo class and the DirectoryInfo class.

The following code demonstrates how to get information about a folder, including getting subfolders under the folder, and the files under the folder. This is done using the DirectoryInfo class, but you can also use directory static classes.
Copy Code code 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 date the file was created, the size of the file, and so on. Of course, you can also use the file static class to complete.
Copy Code code 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 both the file class and the FileInfo class to demonstrate how to delete a file
Copy Code code 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 both the directory class and the DirectoryInfo class to demonstrate how to delete a folder
Copy Code code 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 both the file class and the FileInfo class to demonstrate how to move a file
Copy Code code 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 both the directory class and the DirectoryInfo class to demonstrate how to move a folder
Copy Code code 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 both the file class and the FileInfo class to demonstrate how to copy files
Copy Code code 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);
}
}

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.