Home Programs How To View the Date and time of Access and creation of a File in C#

How To View the Date and time of Access and creation of a File in C#

In this example we will show how to get the time when a file was created, last accessed and last written to in C#

Example

We get the date and time of the file creation, last access and last write times are obtained using the FileInfo class functions.

CreationTime is used to get or set the creation time of the current file or directory.

LastAccessTime is used to get or set the time of the current file or directory last accessed.

LastWriteTime is used to get or set the time when the current file or directory was last written to.

You can change the file to be checked – J:\\csharp\\testfile.txt

using System;
using System.IO;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            FileInfo info = new FileInfo("J:\\csharp\\testfile.txt");
            DateTime time = info.CreationTime;
            Console.WriteLine("File Creation Time     : {0}", time);
            time = info.LastAccessTime;
            Console.WriteLine("File Last Access Time  : {0}", time);
            time = info.LastWriteTime;
            Console.WriteLine("File Last Write Time   : {0} ", time);
            Console.Read();
        }
    }
}

When you run this you will see something like this depending on the file you check

File Creation Time     : 08/05/2022 09:45:22
File Last Access Time  : 08/05/2022 09:47:23
File Last Write Time   : 08/05/2022 09:45:22

You may also like