[Original] C # detailed File operations (1) --- File class

Source: Internet
Author: User

C # File operations are quite convenient, mainly involving four categories: File, FileInfo, Directory, and DirectoryInfo. The first two provide File-specific operations, and the last two provide Directory-specific operations, the class graph relationship is as follows:

Figure 1: class diagram

The following example shows how to use each class.

I. Many static methods are provided in File files. Using these static methods, we can easily perform basic operations such as reading and writing files.

1. Read files

// Abstract: open a file, read the content of the file into a string, and then close the file. Public static byte [] ReadAllBytes (string path); // Abstract: open a text file, read all the lines of the file, and close the file. Public static string [] ReadAllLines (string path); // Abstract: open a file, read all rows of the file using the specified encoding, and close the file. Public static string [] ReadAllLines (string path, Encoding encoding); // Abstract: open a text file, read all lines of the file, and close the file. Public static string ReadAllText (string path); // Abstract: open a file, read all rows of the file using the specified encoding, and close the file. Public static string ReadAllText (string path, Encoding encoding); // Abstract: Read the text line of the object. Public static IEnumerable <string> ReadLines (string path); // Abstract: Read the text lines of files with the specified encoding. Public static IEnumerable <string> ReadLines (string path, Encoding encoding );

2. Write files

// Abstract: Create a new file, write the specified byte array in it, and then close the file. If the target file already exists, overwrite the file. Public static void WriteAllBytes (string path, byte [] bytes); // Abstract: Create a new file, write a set of strings in it, and then close the file. Public static void WriteAllLines (string path, IEnumerable <string> contents); // Abstract: Creates a new file, writes a specified string array to it, and closes the file. Public static void WriteAllLines (string path, string [] contents); // Abstract: Create a new file using the specified encoding, write a set of strings in it, and then close the file. Public static void WriteAllLines (string path, IEnumerable <string> contents, Encoding encoding); // Abstract: Create a new file and write the specified string array in the file using the specified Encoding, close the file. Public static void WriteAllLines (string path, string [] contents, Encoding encoding); // Abstract: Create a new file, write the specified string in it, and then close the file. If the target file already exists, overwrite the file. Public static void WriteAllText (string path, string contents); // Abstract: Create a new file, write the specified string in it, and then close the file. If the target file already exists, overwrite the file. Public static void WriteAllText (string path, string contents, Encoding encoding );

3. Add content

// Abstract: append a text line to a file and close the file. Public static void AppendAllLines (string path, IEnumerable <string> contents); // Abstract: append a text line to a file using the specified encoding, and then close the file. Public static void AppendAllLines (string path, IEnumerable <string> contents, Encoding encoding); // Abstract: open a file, append the specified string to it, and then close the file. If the file does not exist, this method creates a file, writes the specified string to the file, and closes the file. Public static void AppendAllText (string path, string contents); // Abstract: append the specified string to the file. If the file does not exist, create the file. Public static void AppendAllText (string path, string contents, Encoding encoding); // Abstract: creates a System. IO. StreamWriter that appends the UTF-8 Encoding text to an existing file. Public static StreamWriter AppendText (string path );

4. Create a file

// Abstract: Create or overwrite a file in the specified path. Public static FileStream Create (string path); // Abstract: Creates or overwrites the specified file. Public static FileStream Create (string path, int bufferSize); // Abstract: Create or overwrite the specified file, specify the buffer size, and a System that describes how to Create or overwrite the file. IO. fileOptions value. Public static FileStream Create (string path, int bufferSize, FileOptions options); // Abstract: Creates or overwrites a specified file with the specified buffer size, file options, and file security. Public static FileStream Create (string path, int bufferSize, FileOptions options, FileSecurity fileSecurity );

5. Open a file

// Abstract: Open System. IO. FileStream in the specified path and have the read/write access permission. Public static FileStream Open (string path, FileMode mode); // Abstract: Use the specified mode and access permission to Open System. IO. FileStream in the specified path. Public static FileStream Open (string path, FileMode mode, FileAccess); // Abstract: Open the System in the specified path. IO. fileStream, with the specified read, write or read/write access mode and the specified sharing options. Public static FileStream Open (string path, FileMode mode, FileAccess access, FileShare share); // Abstract: Open an existing file for reading. Public static FileStream OpenRead (string path );

6. Get and set file attributes

// Abstract: gets a System. Security. AccessControl. FileSecurity object that encapsulates the access control list (ACL) entries of the specified file. Public static FileSecurity GetAccessControl (string path); // Abstract: gets a System. Security. AccessControl. FileSecurity object that encapsulates the specified type of access control list (ACL) items of a specific file. Public static FileSecurity GetAccessControl (string path, AccessControlSections includeSections); // Abstract: gets the System. IO. FileAttributes of the file on this path. Public static FileAttributes GetAttributes (string path); // Abstract: returns the creation date and time of the specified file or directory. Public static DateTime GetCreationTime (string path); // Abstract: returns the creation date and time of the specified file or directory. The format is Coordinated Universal Time (UTC ). Public static DateTime GetCreationTimeUtc (string path); // Abstract: returns the date and time of the last access to the specified file or directory. Public static DateTime GetLastAccessTime (string path); // Abstract: returns the date and time of the last access to the specified file or directory. The format is Coordinated Universal Time (UTC ). Public static DateTime GetLastAccessTimeUtc (string path); // Abstract: returns the date and time when the specified file or directory was last written. Public static DateTime GetLastWriteTime (string path); // Abstract: returns the date and time of the last write to the specified file or directory. The format is Coordinated Universal Time (UTC ). Public static DateTime GetLastWriteTimeUtc (string path); // Abstract: applies the access control list (ACL) item described by System. Security. AccessControl. FileSecurity to the specified file. Public static void SetAccessControl (string path, FileSecurity fileSecurity); // Abstract: sets the specified System. IO. FileAttributes of the file in the specified path. Public static void SetAttributes (string path, FileAttributes fileAttributes); // Abstract: set the date and time when the file was created. Public static void SetCreationTime (string path, DateTime creationTime); // Abstract: sets the date and time of file creation, in the format of Coordinated Universal Time (UTC ). Public static void SetCreationTimeUtc (string path, DateTime creationTimeUtc); // Abstract: set the date and time of the last access to the specified file. Public static void SetLastAccessTime (string path, DateTime lastAccessTime); // Abstract: sets the date and time of the last object to be accessed. The format is Coordinated Universal Time (UTC ). Public static void SetLastAccessTimeUtc (string path, DateTime lastAccessTimeUtc); // Abstract: set the date and time of the last specified file written. Public static void SetLastWriteTime (string path, DateTime lastWriteTime); // Abstract: sets the date and time of the last specified file written, in the Coordinated Universal Time (UTC) format ). Public static void SetLastWriteTimeUtc (string path, DateTime lastWriteTimeUtc );

7. Copy, move, and replace

// Abstract: copy an existing file to a new file. Files with the same name cannot be overwritten. Public static void Copy (string sourceFileName, string destFileName); // Abstract: Copy an existing file to a new file. The file with the same name can be overwritten. Public static void Copy (string sourceFileName, string destFileName, bool overwrite); // Abstract: Move the specified file to a new location and provide the option to specify a new file name. Public static void Move (string sourceFileName, string destFileName); // Abstract: Replace the content of a specified file with the content of another file. This process deletes the original file, and create a backup of the replaced file. Public static void Replace (string sourceFileName, string destinationFileName, string destinationBackupFileName); // Abstract: Replace the content of a specified file with the content of another file and delete the original file, create a backup for the replaced file and (optional) Ignore the merge error. Public static void Replace (string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors );

8. encryption, decryption, deletion, and determination of existence

// Abstract: encrypts a file so that only the account that encrypts the file can decrypt it. Public static void Encrypt (string path); // Abstract: decrypts a File encrypted by the current account using the System. IO. File. Encrypt (System. String) method. Public static void Decrypt (string path); // Abstract: delete a specified object. If the specified file does not exist, the public static void Delete (string path) exception is not thrown; // Abstract: determines whether the specified file exists. Public static bool Exists (string path );

Through the above function declaration, we should be very clear about how these methods are good. Here we will not illustrate them here.

At the same time, when we see so many functions, we are also very clear that the File class can already meet our basic needs for File operations. The File class provides a way to operate files through static methods.

This article is written here, hoping that the information will help you in file operations.

 

------------------------------------------------------------------------

Me: 277241073@qq.com

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.