FileInfo In C#

C# FileInfo. This article will explain how to use a FileInfo class in C# to work with files and folders.

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.

FileInfo.Create

 
This method is used to create the new file.
  1. static void Main(string[] args) {  
  2.     string path = @ "D:\MyTestFile1.txt";  
  3.     FileInfo fl = new FileInfo(path);  
  4.     File.Create(path); {  
  5.         Console.WriteLine("File has been created");  
  6.     }  
  7. }  

FileInfo.CreateText

This Method creates a StreamWriter that writes a new text file.
  1. FileInfo fi = new FileInfo(@ "D:\MyTestFilecreatetext1.txt");  
  2. StreamWriter str = fi.CreateText();  
  3. str.WriteLine("hello");  
  4. Console.WriteLine("File has been created with text");  
  5. str.Close();  

FileInfo.Delete

 
This method is used to delete an existing file.
  1. FileInfo fi = new FileInfo(@ "D:\MyTestFilecreatetext.txt");  
  2. fi.Delete();  
  3. Console.WriteLine("File has been deleted");  

FileInfo.CopyTo

 
The CopyTo method is used to copy an existing file into a new file. 
  1. string path = @ "d:\MyTestFile7.txt";  
  2. string path2 = @ "d:\NewFile.txt";  
  3. FileInfo fi1 = new FileInfo(path);  
  4. FileInfo fi2 = new FileInfo(path2);  
  5. fi1.CopyTo(path2);  
  6. Console.WriteLine("{0} was copied to {1}.", path, path2);  

FileInfo.MoveTo

 
The MoveTo method is used to move the file from one place to another valid location.
  1. string path = @ "d:\NewFile1.txt";  
  2. string path2 = @ "E:\NewFile1.txt";  
  3. FileInfo fi1 = new FileInfo(path);  
  4. FileInfo fi2 = new FileInfo(path2);  
  5. fi1.MoveTo(path2);  
  6. Console.WriteLine("{0} was copied to {1}.", path, path2);  

FileInfo.AppendText

 
This method creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.
  1. FileInfo fi = new FileInfo(@ "E:\NewFile1.txt");  
  2. StreamWriter sw = fi.AppendText();  
  3. sw.WriteLine("This");  
  4. sw.WriteLine("is Extra");  
  5. sw.WriteLine("Text");  
  6. Console.WriteLine("File has been appended");  
  7. sw.Close();  

FileInfo.OpenText

 
This method creates a StreamReader with UTF8 encoding that reads from an existing text file.
  1. FileInfo fi = new FileInfo(@ "E:\NewFile1.txt");  
  2. StreamReader sr = fi.OpenText();  
  3. string s = "";  
  4. while ((s = sr.ReadLine()) != null) {  
  5.     Console.WriteLine(s);  
  6. }  

FileInfo Code Example

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;  
  6. namespace fileinfoclass1 {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             // Create Method  
  10.             FileInfo fi = new FileInfo(@ "D:\MyTestFile7.txt");  
  11.             FileStream fs = fi.Create();  
  12.             Console.WriteLine("File has been created");  
  13.             //CreateText Method  
  14.             FileInfo fi = new FileInfo(@ "D:\MyTestFilecreatetext1.txt");  
  15.             StreamWriter str = fi.CreateText();  
  16.             str.WriteLine("hello");  
  17.             Console.WriteLine("File has been created with text");  
  18.             str.Close();  
  19.             //Delete Method  
  20.             FileInfo fi = new FileInfo(@ "D:\MyTestFilecreatetext.txt");  
  21.             fi.Delete();  
  22.             Console.WriteLine("File has been deleted");  
  23.             //CreateText Method  
  24.             string path = @ "d:\MyTestFile7.txt";  
  25.             string path2 = @ "d:\NewFile.txt";  
  26.             FileInfo fi1 = new FileInfo(path);  
  27.             FileInfo fi2 = new FileInfo(path2);  
  28.             fi1.CopyTo(path2);  
  29.             Console.WriteLine("{0} was copied to {1}.", path, path2);  
  30.             // MoveTo Method  
  31.             string path = @ "d:\NewFile1.txt";  
  32.             string path2 = @ "E:\NewFile1.txt";  
  33.             FileInfo fi1 = new FileInfo(path);  
  34.             FileInfo fi2 = new FileInfo(path2);  
  35.             fi1.MoveTo(path2);  
  36.             Console.WriteLine("{0} was moved to {1}.", path, path2);  
  37.             //ApendText Method  
  38.             FileInfo fi = new FileInfo(@ "E:\NewFile1.txt");  
  39.             StreamWriter sw = fi.AppendText();  
  40.             sw.WriteLine("This");  
  41.             sw.WriteLine("is Extra");  
  42.             sw.WriteLine("Text");  
  43.             Console.WriteLine("File has been appended");  
  44.             sw.Close();  
  45.             // Opentext Method  
  46.             FileInfo fi = new FileInfo(@ "E:\NewFile1.txt");  
  47.             StreamReader sr = fi.OpenText();  
  48.             string s = "";  
  49.             while ((s = sr.ReadLine()) != null) {  
  50.                 Console.WriteLine(s);  
  51.             }  
  52.         }  
  53.     }  
  54. }  

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.
 

FileInfo Properties

 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;  
  6. namespace fileinfoclass1 {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             FileInfo fi = new FileInfo(@ "E:\NewFile1.txt");  
  10.             Console.WriteLine("File name is {0} ", fi.Name);  
  11.             Console.WriteLine("File creation time is {0} ", fi.CreationTime.ToLongTimeString());  
  12.             Console.WriteLine("File Lastaccesstime is {0} ", fi.LastAccessTime.ToLongDateString());  
  13.             Console.WriteLine("File length is {0} ", fi.Length.ToString() + " Bytes");  
  14.             Console.WriteLine("File extension is {0} ", fi.Extension);  
  15.             Console.WriteLine("File exist is: ", fi.Exists);  
  16.             Console.WriteLine("File LastWriteTime is {0} ", fi.LastWriteTime);  
  17.         }  
  18.     }  
  19. }  

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.