find the last modified date of a file in csharp

You can use the File.GetLastWriteTime method to retrieve the last modified date of a file in C#:

main.cs
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string filePath = @"C:\example\test.txt";
        DateTime lastModified = File.GetLastWriteTime(filePath);
        Console.WriteLine($"Last modified: {lastModified}");
    }
}
274 chars
13 lines

In this example, we first specify the file path for which we want to retrieve the last modified date. We use this path as an argument to the File.GetLastWriteTime method, which returns a DateTime object representing the file's last modified date. Finally, we display this date using Console.WriteLine.

gistlibby LogSnag