C# FileInfo

In c#, FileInfo is a class of System.IO namespace, and it is useful to perform file operations such as create, read, delete, rename, moving, opening, and appending to files.

 

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.

 

In c#, the FileInfo class has different types of properties and methods to perform operations on files to manipulate based on our requirements.

C# FileInfo Properties

The following are the different types of properties provided by the FileInfo class to retrieve the information about files.

 

PropertyDescription
Directory This property will return an instance of the parent directory of a file.
DirectoryName It is useful to retrieve the full path of the parent directory of the file.
Exists This property will return a value that indicates whether the file exists or not.
IsReadOnly This property is useful to get or set a value that determines whether the current file can modify or not.
Length This property will return the size of the current file in bytes.
Name It is useful to get the name of the file.
Extension This will return an extension part of the file.
CreationTime It is useful to get or set the creation time of the current file or directory.
LastAccessTime It is useful to get or set the last access time of the current file or directory.
LastWriteTime It is useful to get or set the time when the current file or directory was last written to.

C# FileInfo Methods

The following are the different types of methods provided by FileInfo class to perform different types of operations on files.

 

MethodDescription
Create This method will create a file.
CopyTo(String) This method will 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 This method will delete the file permanently.
Open It opens a file in the specified mode.
OpenRead It creates a read-only file stream.
OpenWrite It creates a write-only file stream.
OpenText It creates a stream reader that reads from an existing text file.
Replace This will replace the contents of the specified file with the file described by the current fileinfo object.
ToString 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.

C# FileInfo Class Create File Example

Following is the example of creating and writing text to the file using the FileInfo class in c#.

 

using System.IO;

namespace TutlaneExamples
{
    class Program
    {
       static void Main(string[] args)
       {
          string fpath = @"D:\Test.txt";
          // Check file if exists
          if (File.Exists(fpath))
          {
             File.Delete(fpath);
          }
          // Create the file
          FileInfo fi = new FileInfo(fpath);
          //fi.Create();
          // Create and write data to file
          using (StreamWriter sw = fi.CreateText())
          {
             sw.WriteLine("Hi");
             sw.WriteLine("\r\nWelcome to Tutlane");
             sw.WriteLine("\r\nFileInfo Example");
          }
       }
    }
}

If you observe the above example, we imported a System.IO namespace to access File, FileInfo & StreamWriter objects to delete, create and write a text to file.

 

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.

 

C# FileInfo Create and Write Text to File Example Result

 

This is how we can create, delete and add text to files using File, FileInfo & StreamWriter objects in c#.

C# FileInfo Class Read File Example

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.

 

using System;
using System.IO;

namespace TutlaneExamples
{
    class Program
    {
        static void Main(string[] args)
        {
           string fpath = @"D:\Test.txt";
           // Check if file exists
           if (File.Exists(fpath))
           {
              FileInfo fi = new FileInfo(fpath);
              // open the file to read text
              using (StreamReader sr = fi.OpenText())
              {
                 string txt;
                 // Read the data from file, until the end of file is reached
                 while ((txt = sr.ReadLine()) != null)
                 {
                    Console.WriteLine(txt);
                 }
              }
           }
           Console.ReadLine();
        }
    }
}

If you observe the above example, we imported a System.IO namespace to access FileInfo & StreamReader object to open and read text from the given file.

 

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.

 

C# FileInfo Read Text from File Example Result

 

This is how you can use the FileInfo class in c# to create and read the data from a file in the file system.