Online Training for MVC Web Designing Development, MS SQL, Digital Marketing

Learn FileInfo in C# .Net Tutorial

System.IO.FileInfo class in C#

In this article you will learn about Fileinfo class in C# .Net, How to create file, delete file using Fileinfo in C# .Net .
FileInfo class in .Net comes under System.IO namespace.

The FileInfo class provides the almost same functionality as the static System.IO.File class in .Net, in FileInfo we have more to do on read/write operations on files by manually implementing code.

Properties and Methods of FileInfo
Here are some very useful properties and method of System.IO.FileInfo class, best way to understand following properties of FileInfo class, create a new instance of FileInfo
Create a plain txt file in your local machine with some content inside, thre create a new object
    //Create a new instance of FileInfo for specified file  
    FileInfo fi = new FileInfo(@"D:\myfile.txt");
Now simply type fi., you will be able to see all properties and methods
Property Description
DirectoryName Get the Directory name of the file (full path)
Directory Get the DirectoryInfo object, there you can get all information about that Directory
Exists Before you work with any file, you can check if file exists
Extension Get the file extension, like .txt etc.
Method Description
Create Creates a new file
Delete Deletes the specified file permanently
MoveTo Move any specified file to a new location
Encrypt Encrypt the file so that other account user can't open the file
Decrypt Decrypt the file that was encrypted by the same account it was Encrypted with
GetAccessControl Gets a FileSecurity object of a specified file.
Replace Replace the contents of a specified file, delete the original file, and create a backup of old file.
AppendText Create a StreamWriter object that appends text to the file instance of the FileInfo class.

Create a file using FileInfo in C#

Here we create a new file with some content
//  File name with full path   
string fileName= @"D:\application\myfile.txt";              
FileInfo fi = new FileInfo(fileName);

// Create a new file     
using (FileStream fs = fi.Create())
    Byte[] txt = new UTF8Encoding(true).GetBytes("We are adding some text in the file");
    fs.Write(txt, 0, txt.Length);

    Byte[] author = new UTF8Encoding(true).GetBytes("WebTrainingRoom.Com");
    fs.Write(author, 0, author.Length);

 // to check if file exists 
if (fi.Exists)
    // Get file name with full path     
    string fullFileName = fi.Name;
    Console.WriteLine("File Name: {0}", fullFileName);

    // Get file extension     
    string fileExtension = fi.Extension;
    Console.WriteLine("File Extension: {0}", fileExtension);

    // Get directory object     
    DirectoryInfo directory = fi.Directory;
    Console.WriteLine("Directory: {0}", directory.Name); 

    // Get directory name     
    string directoryName = fi.DirectoryName;
    Console.WriteLine("Directory Name: {0}", directoryName); 

FileInfo Class in C#
Learn C# Online Tutorial

Group Training