Working With the FileInfo Class in C#

Introduction
 

This article will explain the FileInfo class and its use. The System.IO namespace is one of the most significant namespaces used for working with Files in the .Net Framework. Let us see about the class.
 
FileInfo Class
 
The FileInfo class does not have static methods and can only be used on instantiated objects. The FileInfo object represents a file on a disk or network location. It also provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. Some of the most useful methods of the FileInfo class are:
 
List of Methods of FileInfo Class, I have included only a few methods of the FileInfo Class.
 
S.No Methods Description
1 Create
 
This method is used to create the new file.
2 CreateText This Method Creates a StreamWriter that writes a new text file.
3 Delete This method is used to delete a existing file.
4 CopyTo The CopyTo method is used to copy the existing file into new file. 
5 MoveTo The MoveTo method is used to move the file one place to another valid location.
6 AppendText This method creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.
7 OpenText This method creates a StreamReader with UTF8 encoding that reads from an existing text file.

 
The methods of the FileInfo class.
 
Create Method
 
This method is used to create the new file.
 

static void Main(string[] args)

{

    string path = @"D:\MyTestFile1.txt";

    FileInfo fl = new FileInfo(path);

    File.Create(path);

    {

         Console.WriteLine("File has been created");

     }    

}


 
CreateText
 
This Method creates a StreamWriter that writes a new text file.
 

FileInfo fi = new FileInfo(@"D:\MyTestFilecreatetext1.txt");

StreamWriter str = fi.CreateText();

str.WriteLine("hello");

Console.WriteLine("File has been created with text");

str.Close();

 

Delete
 
This method is used to delete an existing file.
 

FileInfo fi = new FileInfo(@"D:\MyTestFilecreatetext.txt");

 fi.Delete();

Console.WriteLine("File has been deleted");

 
CopyTo
 
The CopyTo method is used to copy an existing file into a new file. 
 

string path = @"d:\MyTestFile7.txt";

string path2 = @"d:\NewFile.txt";

FileInfo fi1 = new FileInfo(path);

FileInfo fi2 = new FileInfo(path2);

fi1.CopyTo(path2);

Console.WriteLine("{0} was copied to {1}.", path, path2);  

 
MoveTo
 
 The MoveTo method is used to move the file from one place to another valid location.
 

string path = @"d:\NewFile1.txt";

string path2 = @"E:\NewFile1.txt";

FileInfo fi1 = new FileInfo(path);

FileInfo fi2 = new FileInfo(path2);

fi1.MoveTo(path2);  Console.WriteLine("{0} was copied to {1}.", path, path2);

 
AppendText
 
This method creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.
 

FileInfo fi = new FileInfo(@"E:\NewFile1.txt");

StreamWriter sw = fi.AppendText();

sw.WriteLine("This");

sw.WriteLine("is Extra");

sw.WriteLine("Text");

Console.WriteLine("File has been appended");

sw.Close();

 
OpenText
 
This method creates a StreamReader with UTF8 encoding that reads from an existing text file.
 

FileInfo fi = new FileInfo(@"E:\NewFile1.txt");

StreamReader sr = fi.OpenText();

string s = "";

while ((s = sr.ReadLine()) != null)

{

     Console.WriteLine(s);

}

 

Example

 

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(@"D:\MyTestFile7.txt");

            FileStream fs = fi.Create();

            Console.WriteLine("File has been created");

 

 //CreateText Method

 

            FileInfo fi = new FileInfo(@"D:\MyTestFilecreatetext1.txt");

            StreamWriter str = fi.CreateText();

            str.WriteLine("hello");

            Console.WriteLine("File has been created with text");

            str.Close();

 

//Delete Method

 

            FileInfo fi = new FileInfo(@"D:\MyTestFilecreatetext.txt");

            fi.Delete();

            Console.WriteLine("File has been deleted");

 

//CreateText Method

 

            string path = @"d:\MyTestFile7.txt";

            string path2 = @"d:\NewFile.txt";

            FileInfo fi1 = new FileInfo(path);

            FileInfo fi2 = new FileInfo(path2);

            fi1.CopyTo(path2);

            Console.WriteLine("{0} was copied to {1}.", path, path2);

 

 // MoveTo Method

            string path = @"d:\NewFile1.txt";

            string path2 = @"E:\NewFile1.txt";

            FileInfo fi1 = new FileInfo(path);

            FileInfo fi2 = new FileInfo(path2);

            fi1.MoveTo(path2);

            Console.WriteLine("{0} was moved to {1}.", path, path2);

           

//ApendText Method

 

            FileInfo fi = new FileInfo(@"E:\NewFile1.txt");

            StreamWriter sw = fi.AppendText();

            sw.WriteLine("This");

            sw.WriteLine("is Extra");

            sw.WriteLine("Text");

            Console.WriteLine("File has been appended");

            sw.Close();

 

// Opentext Method

 

            FileInfo fi = new FileInfo(@"E:\NewFile1.txt");

            StreamReader sr = fi.OpenText();

            string s = "";

            while ((s = sr.ReadLine()) != null)

            {

                Console.WriteLine(s);

            }

        }

    }

} 

 

The FileInfo class provides the following properties that enable you to retrieve information about a file.
 

S.No Property Description
1 CreationTime This property returns the creation of the file date and time.
2 Exists The Exists property checks for the presence of a file before operating on it.
3 Extension It returns the type of the file in the extension name
4 FullName It returns the full name of the file from the root directory.
5 Name The Name retrieves the name of a file.
6 LastWriteTime It returns the last file saving date and time
7 Length The Length retrieves the size of a file.
 
Property - Example
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace fileinfoclass1

{

    class Program

    {

        static void Main(string[] args)

        {

 

            FileInfo fi = new FileInfo(@"E:\NewFile1.txt");

            Console.WriteLine("File name is {0} ", fi.Name);

            Console.WriteLine("File creation time is {0} ", fi.CreationTime.ToLongTimeString());

            Console.WriteLine("File Lastaccesstime is {0} ",                      fi.LastAccessTime.ToLongDateString());

            Console.WriteLine("File length is {0} ", fi.Length.ToString() + " Bytes");

            Console.WriteLine("File extension is {0} ", fi.Extension);

            Console.WriteLine("File exist is: ", fi.Exists);

            Console.WriteLine("File LastWriteTime is {0} ", fi.LastWriteTime);            

        }

    }

}

 

Conclusion

The FileInfo class is very useful for working with files. If there is any mistake in this article then please notify me. I expect your valuable comments and feedback about this article.

Up Next
    Ebook Download
    View all
    Learn
    View all