Retrieve Details of file using C# FileInfo class

Updated on     Kisan Patel

The .NET Framework provides certain classes to work with files, such as the File and FileInfo classes. These classes enable a user to perform different operations on files programmatically. The File and FileInfo classes allow you to perform various functions, such as creating, deleting, moving, renaming, and appending files.

The basic difference between these two classes is that the File class provides static methods, whereas the FileInfo class needs to create its instances. It is advisable to use the FileInfo class instead of the File class when we want to perform multiple operations on a file.

NAMESPACE:

using System.IO;

CODE:

class Program
{
     static void Main(string[] args)
     {
         string path = @"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg";
         FileInfo file = new FileInfo(path);
         Console.WriteLine("File Attributes : " + file.Attributes + "\nCreated At: " + File.GetCreationTimeUtc(path) + "\n Last Accessed At:" + file.LastAccessTimeUtc + "\n File Name:" + file.FullName + "\n File Extension:" + file.Extension);
     }
}

the output of the above C# program…

fileinfo_class_example

Using File class in C#

NAMESPACE:

using System.IO;

CODE:

string path = @"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg";
Console.WriteLine("File Attributes : " + File.GetAttributes(path) + "\nCreated At: " + File.GetCreationTimeUtc(path) + "\n Last Accessed At:" + File.GetLastAccessTimeUtc(path));

C#

Leave a Reply