72

I have an .NET EXE file . I want to find the file created date and modified date in C# application. Can do it through reflection or with IO stream?

Anas Alweish
  • 2,010
  • 3
  • 20
  • 36
griffithstratton
  • 731
  • 1
  • 5
  • 5

6 Answers6

136

You could use below code:

DateTime creation = File.GetCreationTime(@"C:\test.txt");
DateTime modification = File.GetLastWriteTime(@"C:\test.txt");
waitefudge
  • 1,361
  • 1
  • 8
  • 3
38

You can do that using FileInfo class:

FileInfo fi = new FileInfo("path");
var created = fi.CreationTime;
var lastmodified = fi.LastWriteTime;
Selman Genç
  • 94,267
  • 13
  • 106
  • 172
  • Per the link, "If you are performing multiple operations on the same file, it can be more efficient to use FileInfo instance methods instead of the corresponding static methods of the File class, because a security check will not always be necessary." – VoteCoffee Feb 12 '20 at 18:23
9

File.GetLastWriteTime to Get last modified

File.CreationTime to get Created time

Sajeetharan
  • 186,121
  • 54
  • 283
  • 331
6

Use :

FileInfo fInfo = new FileInfo('FilePath');
var fFirstTime = fInfo.CreationTime;
var fLastTime = fInfo.LastWriteTime;
DOTNET Team
  • 3,391
  • 17
  • 30
5

File.GetLastWriteTime Method

Returns the date and time the specified file or directory was last written to.

string path = @"c:\Temp\MyTest.txt";
DateTime dt = File.GetLastWriteTime(path);

For create time File.GetCreationTime Method

DateTime fileCreatedDate = File.GetCreationTime(@"C:\Example\MyTest.txt");
Console.WriteLine("file created: " + fileCreatedDate);
Nagaraj S
  • 12,563
  • 6
  • 30
  • 50
4

You can use this code to see the last modified date of a file.

DateTime dt = File.GetLastWriteTime(path);

And this code to see the creation time.

DateTime fileCreatedDate = File.GetCreationTime(@"C:\Example\MyTest.txt");
markieo
  • 432
  • 3
  • 12