How to get file creation and modification time in c# ?

To get file datetime info you can use either static methods of File class or instance methods of FileInfo class.

1) Using File class to get file times

// local times
DateTime creationTime = File.GetCreationTime(@"F:\abcfile.txt");
DateTime lastWriteTime = File.GetLastWriteTime(@"F:\abcfile.txt");
DateTime lastAccessTime = File.GetLastAccessTime(@"F:\abcfile.txt");

// UTC times
DateTime creationTimeUtc = File.GetCreationTimeUtc(@"F:\abcfile.txt");
DateTime lastWriteTimeUtc = File.GetLastWriteTimeUtc(@"F:\abcfile.txt");
DateTime lastAccessTimeUtc = File.GetLastAccessTimeUtc(@"F:\abcfile.txt");

//write file last modification time (local / UTC)
Console.WriteLine(lastWriteTime);     // 7/04/2018 9:12:03 PM

2) Using FileInfo class to get file times

FileInfo fileInfo = new FileInfo(@"F:\abcfile.txt");

// local times
DateTime creationTime = fileInfo.CreationTime;
DateTime lastWriteTime = fileInfo.LastWriteTime;
DateTime lastAccessTime = fileInfo.LastAccessTime;

// UTC times
DateTime creationTimeUtc = fileInfo.CreationTimeUtc;
DateTime lastWriteTimeUtc = fileInfo.LastWriteTimeUtc;
DateTime lastAccessTimeUtc = fileInfo.LastAccessTimeUtc;

// write file last modification time (local / UTC)
Console.WriteLine(lastWriteTime);     // 07/04/2018 9:12:03 PM
Console.WriteLine(lastWriteTimeUtc);  // 07/04/2018  9:12:03 PM
About Author: author 4 + years of Information Technology experience in understanding and analyzing the software and hardware engineering requirements, onsite and offshore management of product development. Expert work on design and development for windows,web,mobile Expert knowledge of C#,ADO.NET, ASP.Net MVC, ENTITY FRAMEWORK, LINQ,COLLECTIONS, JAVASCRIPT,AngularJs 1.0,Angular2, Jquery,Jquery-Ajax, SQL SERVER, XAML,Windows phone 8.1 Good knowledge of WCF-Restful services,WCF, WEB API-Services ,Web- Services,MIcrosoft-Azure,JUICE UI,HTML,CSS,AJAX Read More...

Join him on Google+ | Facebook | Linkedin

No comments