The FileInfo class of C# provides the same functions as the File class. The difference is that FileInfo provides all member methods, and the usage example is as follows:
1. Read the file:
//Create read-only.public OpenRead() //Create it to read from an existing text file using UTF8 encoding.public OpenText()
2. Write a file:
//Create write-only.public OpenWrite()
3. Additional content:
//Create a , which appends text to the file represented by this instance of .public AppendText()
4. Open the file:
//Open the file in the specified mode.public Open( mode) //Open the file in the specified mode with read, write or read/write access permissions.public Open( mode, access) //Open the file in the specified mode with read, write or read/write access and specified sharing options. public Open( mode, access, share)
5. Copy, move, replace:
//Copy existing files to new files, and overwriting existing files is not allowed.public CopyTo(string destFileName) //Copy existing files to new files, allowing overwriting of existing files.public CopyTo(string destFileName, bool overwrite) //Move the specified file to a new location and provide the option to specify the new file name.public void MoveTo(string destFileName) //Replace the contents of the specified file with the file described by the current object. This process will delete the original file and create a backup of the replaced file.public Replace(string destinationFileName, string destinationBackupFileName) //Replace the contents of the specified file with the file described by the current object. This process will delete the original file and create a backup of the replaced file. Also specify whether to ignore the merge error.public Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
6. Encryption and decryption, deletion:
//Encrypt a file so that only the account that encrypts the file can decrypt it.public void Encrypt() //Decrypt files encrypted by the current account using the () method. public void Decrypt() //Permanently delete the file.public override void Delete()
7. Obtain file attributes:
//Get the instance of the parent directory.public Directory { get; } //Get the string representing the full path of the directory.public string DirectoryName { get; } //Get the value indicating whether the file exists.public override bool Exists { get; } //Get or set the value to determine whether the current file is read-only.public bool IsReadOnly { set; get; } //Get the size (bytes) of the current file.public long Length { get; } //Get the file name.public override string Name { get; }
Getting the relevant attributes of a file in FileInfo is no longer a method, but are all obtained through attributes. Except for whether the read-only attribute is read-only, other attributes are read-only.
Summarize:
Everyone noticed that we areThe methods provided in FileInfo are no longer static, and the return values are of FileStream type, which is a file stream. Therefore, when we use FileInfo class, we also need to use them in conjunction with FileStream class.. And inWhen introducing the File class, all operations are implemented through static methods, and the return values are specific value types.。
This is also a rough comparison between the File class and the FileInfo class.