The FileInfo class will provide the same functionality as the File class to manipulate the files, but if we are performing multiple operations on the single file, then it’s more efficient to use FileInfo class methods instead of File class methods.
The following are the different types of properties provided by the FileInfo class to retrieve the information about files.
|This property will return an instance of the parent directory of a file.
|It is useful to retrieve the full path of the parent directory of the file.
|This property will return a value that indicates whether the file exists or not.
|This property is useful to get or set a value that determines whether the current file can modify or not.
|This property will return the size of the current file in bytes.
|It is useful to get the name of the file.
|This will return an extension part of the file.
|It is useful to get or set the creation time of the current file or directory.
|It is useful to get or set the last access time of the current file or directory.
|It is useful to get or set the time when the current file or directory was last written to.
The following are the different types of methods provided by FileInfo class to perform different types of operations on files.
|This method will create a file.
|This method will copies an existing file to a new file, but it won't allow overwriting an existing file.
|This method will copy an existing file to a new file, allowing overwriting an existing file.
|It creates a StreamWriter that writes a new text file.
|It creates a StreamWriter that appends text to the file.
|This method is useful to encrypt a file so that only the account used to encrypt the file can decrypt it.
|This method is useful to decrypt a file that is encrypted by the current account using the encrypt method.
|This method will delete the file permanently.
|It opens a file in the specified mode.
|It creates a read-only file stream.
|It creates a write-only file stream.
|It creates a stream reader that reads from an existing text file.
|This will replace the contents of the specified file with the file described by the current fileinfo object.
|This will return the path as a string.
Now, we will see how to use FileInfo class in c# to create, delete, read, move and open operations on files with examples.
Following is the example of creating and writing text to the file using the FileInfo class in c#.
When we execute the above example, it will create a new “Test.txt” file in the D drive with the required text as shown below.
This is how we can create, delete and add text to files using File, FileInfo & StreamWriter objects in c#.
In the previous example, we learned how to use the FileInfo class to create and write a text to file. Now, we will learn how to use the FileInfo class to read text from a file with an example.
When you execute the above example, it will read text from the “Test.txt” file in D drive and return the result as shown below.
This is how you can use the FileInfo class in c# to create and read the data from a file in the file system.