In C#, FileInfo class provides properties and instance methods that are used to deal with the various operations in a file such as creating, copying, deleting, moving, etc.
It is derived from FileSystemInfo class and is a part of System.IO namespace.
C# FileInfo Properties
The following are some of the most used properties in FileInfo class.
Property | Description |
---|---|
Directory | It returns an instance of the parent directory of a file. |
DirectoryName | It retrieves the directory full path as a string. |
Exists | It returns a value that indicates whether the file exists or not. |
IsReadOnly | It is used to get or set a value that determines whether the current file is read-only or not. |
Length | This property gets the size of the current file in bytes. |
Name | It is used to get the name of the file. |
Extension | This will return an extension part of the file. |
CreationTime | It is used to get or set the creation time of the current file or directory. |
LastAccessTime | It is used to get or set the last access time of the current file or directory. |
LastWriteTime | It is used to get or set the time when the current file or directory was last written to. |
C# FileInfo Methods
The table below lists the methods used in FileInfo class.
Method | Description |
---|---|
Create() | This method is used to create a file. |
CopyTo(String) | This method copies an existing file to a new file, but it won’t allow overwriting an existing file. |
CopyTo(String, Boolean) | This method will copy an existing file to a new file, allowing overwriting an existing file. |
CreateText() | It creates a StreamWriter that writes a new text file. |
AppendText() | It creates a StreamWriter that appends text to the file. |
Encrypt() | This method is useful to encrypt a file so that only the account used to encrypt the file can decrypt it. |
Decrypt() | This method is useful to decrypt a file that is encrypted by the current account using the encrypt method. |
Delete() | It deletes the file permanently. |
Open() | This method opens a file in the specified mode. |
OpenRead() | This method creates a read-only file stream. |
OpenWrite() | This method creates a write-only file stream. |
OpenText() | This method creates a stream reader that reads from an existing text file. |
Replace() | This method is used to replace the contents of the specified file with the file described by the current fileinfo object. |
ToString() | It returns the path as a string. |
Now, let us go through an example in C# for FileInfo.
Example: C# FileInfo program for creating a file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System; using System.IO; namespace FileProgram { class Program { static void Main(string[] args) { string fLoc = "sample.text"; // Check file if exists if (File.Exists(fLoc)) { File.Delete(fLoc); } FileInfo f = new FileInfo(fLoc); f.Create(); Console.WriteLine("File is created Successfuly"); } } } |
Output: After execution, you will see the following result.
File is created Successfuly
Also, since we created a new file, a file name “sample.text” will be created in the same path where you have saved your code file. Although, if you want to change the directory of the newly created file then you can give the full pathname instead of just the file name.
Example: C# FileInfo program to Write and Read from a file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | using System; using System.IO; namespace FileProgram { class Program { static void Main(string[] args) { string fLoc = "sample.text"; // Check file if exists if (File.Exists(fLoc)) { File.Delete(fLoc); //if file exists then delete that file } FileInfo f = new FileInfo(fLoc); // Writing to the file StreamWriter sw = f.CreateText(); sw.WriteLine("Hi! Welcome to Simple2code.com."); sw.Close(); // Reading from a file StreamReader sr = f.OpenText(); string text = ""; while ((text = sr.ReadLine()) != null) { Console.WriteLine(text); } } } } |
Output: After execution, you will see the following result.
Hi! Welcome to Simple2code.com.
A file will be created and the above text is written on it. The program first writes the string on a text file and then reads from it.