In the C#, FileInfo class is used to deal with files and their operations in C# programming language which provides properties and methods that are used to moving, delete create, and read files. It is a part of the System.IO namespace.
The FileInfo class provides the same functionality as the static File class but we have more control on reading/write operations on files by writing code manually for reading or writing bytes from a file.
C# FileInfo Class Signature
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class FileInfo : FileSystemInfo
C# FileInfo Constructors
The following table contains constructors for the FileInfo class.
Constructor | Description |
FileInfo(String) | Initialize a new instance of the FileInfo class which acts as a wrapper for a file path. |
C# FileInfo Properties
Properties | Description |
Attributes | get or set the attributes for the current file or directory. |
CreationTime | get or set the creation time of the current file or directory. |
Directory | get an instance of the parent directory. |
DirectoryName | get a string representing the directory's full path. |
Exists | get a value indicating whether a file exists. |
FullName | get the full path of the directory or file. |
IsReadOnly | get or set a value that determines if the current file is read only. |
LastAccessTime | get or set the time from the current file or directory was last accessed. |
Length | get the size in bytes of the current file. |
Name | get the name of the file. |
C# FileInfo Methods
Method | Description |
AppendText() | Create a StreamWriter that appends text to the file represented by this instance of the FileInfo. |
CopyTo(String) | Used to copy an existing file to a new file. |
Create() | Ccreate a file. |
CreateText() C | Create a StreamWriter that writes a new text file. |
Decrypt() | Decrypt a file that was encrypted by the current account using the Encrypt method. |
Delete() | Permanently delete a file. |
Encrypt() | Encrypt a file so that only the account used to encrypt the file can decrypt it. |
GetAccessControl() | Get a FileSecurity object that encapsulates the access control list (ACL) entries. |
MoveTo(String) | Move a specified file to a newly specified location. |
Open(FileMode) | Open a file in the specified mode. |
OpenRead() | Create a read-only FileStream. |
OpenText() | Create a StreamReader with UTF8 encoding that reads from an existing text file. |
OpenWrite() | Create a write-only FileStream |
Refresh() | Refresh the state of the object. |
Replace(String,String) | Replace the contents of a specified file with the file described by the current FileInfo object. |
ToString() | Return the path as a string. |
C# FileInfo Example: Creating a File
using System;
using System.IO;
namespace CSharpProgram
{
public class Program
{
public static void Main(string[] args)
{
try
{
// Specifying file location
string loc = "C:\\test.txt";
// Creating FileInfo instance
FileInfo file = new FileInfo(loc);
// Creating an empty file
file.Create();
Console.WriteLine("File is created Successfuly");
}catch(IOException exception) { Console.WriteLine("Something went wrong: "+exception);
}
}
}
}
Output
File is created Successfully
We can see inside the F drive a file test.txt is created. A screenshot image is given below.
C# FileInfo Example: writing to the file
using System;
using System.IO;
namespace CSharpProgram
{
public class Program
{
static void Main(string[] args)
{
try {
// Specifying file location string loc = "C:\\test.txt";
// Creating FileInfo instance
FileInfo file = new FileInfo(loc); // Creating an file instance to write
StreamWriter sw = file.CreateText();
// Writing to the file
sw.WriteLine("Hello Word");
sw.Close();
}catch(IOException exception)
{
Console.WriteLine("Something is going to wrong: "+exception); }
}
}
}
Output
C# FileInfo Example: Reading text from the file
using System;
using System.IO;
namespace CSharpProgram
{
public class Program
{
public static void Main(string[] args)
{
try
{
// Specifying file to read
string loc = "C:\\test.txt"; // Creating FileInfo instance
FileInfo file = new FileInfo(loc);
// Opening file to read
StreamReader sr = file.OpenText();
string data = "";
while ((data = sr.ReadLine()) != null)
{
Console.WriteLine(data);
}
}
catch (IOException exception)
{
Console.WriteLine("Something is going to wrong: " + exception); }
}
}
}