Open In App

C# - FileInfo Class Methods

Last Updated : 09 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will explain the FileInfo class, its methods, properties, etc. The System.IO namespace is one of the most important namespaces we used while we are working with files in C#.

FileInfo Class:

It does not have static methods and it can only be used on instantiated objects. The class objects represent a file on a disk. The FileInfo class also provides instance methods for the creation, copying, deletion, moving, and opening of files.

Methods of FileInfo Class:

MethodsDescription
 CreateThis method is used when we have to create a new file.
CreateTextThis method creates a stream writer whose work is to write a new text file.
Delete This method is used when we have to delete an already existing file.
CopyToThis method is used to copy the existing file into a new file. 
MoveToThis method is used when we have to move a file from one place to another place.
AppendTextThis method creates a stream writer whose work is to append the text to the file represented by the instance of FileInfo class.
OpenTextThis method creates a StreamReader which has a UTF8 encoding whose work is to read from an already existing text file.

Properties of FileInfo Class:

The FileInfo class provides the following properties:

PropertyDescription
CreationTimeIts work is to return the creation of the file date and time.
ExistsThis property checks for the presence of a file before working on it.
ExtensionThis property returns the type of file.
FullNameThis property returns the full name of the file.
NameThis property returns the name of the file.
LastWriteTimeThis property returns the date and time of the last file saved.
LengthThis property retrieves the size of a file.

Below is the C# program which illustrates the methods of FileInfo class. Now, here we will use the FileInfo class to perform the following operations which are: copying, moving, renaming, creating, opening, deleting, and appending to files.

Example 1:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace fileinfoclass1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create Method  
            FileInfo fi = new FileInfo("C:\\Users\\A\\
                                       OneDrive\\Documents\\MyFile1.txt");
            FileStream fs = fi.Create();
            Console.WriteLine("File has been created");


            //CreateText Method  
            FileInfo fit = new FileInfo("C:\\Users\\A\
            \OneDrive\\Documents\\MyFilecreatetext1.txt");
            StreamWriter str = fit.CreateText();
            str.WriteLine("hello");
            Console.WriteLine("File has been created with text");
            str.Close();


            //Delete Method  
            FileInfo fitd = new FileInfo("C:\\Users\\A\\
                                         OneDrive\\Documents\\MyFiledelete1.txt");
            fitd.Delete();
            Console.WriteLine("File has been deleted");


            //CopyTo Method  
            string path = "C:\\Users\\A\\Downloads\\MyFile1.txt";
            string path2 = "C:\\Users\\A\\Downloads\\Newfile1.txt";
            FileInfo fi1 = new FileInfo(path);
            FileInfo fi2 = new FileInfo(path2);
            fi1.CopyTo(path2);
            Console.WriteLine("Copied Successfully", path, path2);


            // MoveTo Method  
            string path3 = "C:\\Users\\A\\OneDrive\\Documents\\Newfile2.txt";
            string path4 = "C:\\Users\\A\\Downloads\\Newfile2.txt";
            FileInfo f3 = new FileInfo(path3);
            FileInfo f4 = new FileInfo(path4);
            f3.MoveTo(path4);
            Console.WriteLine("Moved Successfully", path3, path4);


            //AppendText Method  
            FileInfo fia = new FileInfo("C:\\Users\\A\\Downloads\\Newfile2.txt");
            StreamWriter sw = fia.AppendText();
            sw.WriteLine("This");
            sw.WriteLine("is Extra");
            sw.WriteLine("Text");
            Console.WriteLine("File has been appended");
            sw.Close();


            // Opentext Method  
            FileInfo fio = new FileInfo("C:\\Users\\A\\Downloads\\Newfile2.txt");
            StreamReader sr = fio.OpenText();
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
    }
}

Output:

 

Next Article
Article Tags :

Similar Reads