How to get File Created Date and Modified Date [duplicate]
Asked Answered
G

6

105

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?

Gert answered 23/4, 2014 at 11:46 Comment(2)
Have a look at the File class, should't be that hard: msdn.microsoft.com/en-us/library/system.io.file.aspxProjective
Google gets a lot of result if you searched it first.Broiler
P
190

You could use below code:

DateTime creation = File.GetCreationTime(@"C:\test.txt");
DateTime modification = File.GetLastWriteTime(@"C:\test.txt");
Pirtle answered 23/4, 2014 at 11:51 Comment(2)
if i want to know the same thing with file bytes. how could it be?Wideangle
Note--if it comes back with a minimum date it's probably because the file doesn't exist/invalid path, etc. (It doesn't throw an exception)Tyrontyrone
G
50

You can do that using FileInfo class:

FileInfo fi = new FileInfo("path");
var created = fi.CreationTime;
var lastmodified = fi.LastWriteTime;
Galloromance answered 23/4, 2014 at 11:47 Comment(1)
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."Curling
W
9

File.GetLastWriteTime to Get last modified

File.CreationTime to get Created time

Whorish answered 23/4, 2014 at 11:48 Comment(0)
H
7

Use :

FileInfo fInfo = new FileInfo('FilePath');
var fFirstTime = fInfo.CreationTime;
var fLastTime = fInfo.LastWriteTime;
Handling answered 23/4, 2014 at 11:48 Comment(2)
These things don't stand alone. The are part of a class. Which class?Diaphane
The SystemIO classAbomb
K
6

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);
Knossos answered 23/4, 2014 at 11:52 Comment(0)
U
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");
Upsydaisy answered 23/4, 2014 at 11:50 Comment(1)
just remember, there is a bug going back to the 90s in NTFS - windows OS gui - that mixes the created date with the modified date (ever wonder why they never fixed it?)Emasculate

© 2022 - 2024 — McMap. All rights reserved.