Free Web Development Online Tutorials, Learn to Code
C# FileInfo Class Example, Learn C# programming Language, Free C# Tutorial for Beginners

C# FileInfo Class Example

In this tutorial you will learn how to use C# FileInfo class for all file related operation.

Use 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

C# FileInfo Properties

  • 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

    c# fileinfo file exists example, this method will allow you to check if file exists.

    string fileName = @"D:\application\myfile.txt";
    FileInfo fi = new FileInfo(fileName);
    if (fi.Exists)
        Console.WriteLine(fi.FullName);
    

    Before you work with any file, you can check if file exists

  • Extension
    string fileName = @"D:\application\myfile.txt";
    FileInfo fi = new FileInfo(fileName);
    Console.WriteLine(fi.Extension);
    

    Get the file extension, like .txt etc.

C# FileInfo Methods

  • Create
    string fileName = @"D:\application\myfile.txt";
    FileInfo fi = new FileInfo(fileName);
    fi.Create();
    

    Creates a new file

  • Delete
    string fileName = @"D:\application\myfile.txt";
    FileInfo fi = new FileInfo(fileName);
    fi.Delete();
    

    Deletes the specified file permanently

  • MoveTo

    Move any specified file to a new location, c# fileinfo move file

  • 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 in it, notice how to create byte array and how to write byte array using FileStream class.

//  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);
}

Check if File Exists in C#

Before checking any file information we need to check if file exists, (Also learn how to get file name, extension, DirectoryName) otherwise it will throw null object reference error

//  File name with full path   
string fileName= @"D:\application\myfile.txt";              
      
FileInfo fi = new FileInfo(fileName);
 // 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); 
}
    

We also can set file permission using FileInfo class

Comment
Name
Email
Website
Subscribe
 

FileInfo Class in C#
Other Online Tutorial

C# .net Interview Questions Answers