find the last accessed date of a file in csharp

You can find the last accessed date of a file in C# using the FileInfo class, which provides a LastAccessTime property that returns a DateTime object representing the date and time the file was last accessed. Here's an example:

main.cs
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string path = @"C:\example.txt";
        FileInfo fileInfo = new FileInfo(path);
        DateTime lastAccessed = fileInfo.LastAccessTime;
        
        Console.WriteLine($"Last accessed: {lastAccessed}");
    }
}
314 chars
15 lines

This code creates a FileInfo object for the file at the specified path, then gets the LastAccessTime property of that object to get the date and time the file was last accessed. The resulting DateTime object is stored in the lastAccessed variable, which is then printed to the console.

gistlibby LogSnag