28

I'm using the following to change the creation date of a text file:

using System.IO;

...
DateTime newCreate = new DateTime(year, month, day, hour, minutes, seconds);
File.SetCreationTime("changemydate.txt", newCreate);

However this doesn't do anything. There is no error message, yet it doesn't change the date of the file at all.

I tried this in a dropbox folder as well as in a random folder without success

The DateTime newCreate object seems to be correct though.

It would be great if somebody could point me to an idea...

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
pandita
  • 4,739
  • 6
  • 29
  • 51
  • 2
    Could be [a side-effect of drive format](http://msdn.microsoft.com/en-us/library/system.io.file.setcreationtime.aspx)? _"NTFS-formatted drives may cache file meta-info, such as file creation time, for a short period of time. As a result, it may be necessary to explicitly set the creation time of a file if you are overwriting or replacing an existing file."_ – Grant Thomas Jun 15 '13 at 15:54
  • Thanks Grant Thomas, but how would I do that explicitly? The link sends me to the default File.SetCreationTime method... – pandita Jun 15 '13 at 16:00
  • Are you sure you have rights to set the file creation time? If you don't then you see see a security event in the event log. – Hogan Jun 15 '13 at 17:19
  • 3
    How are you verifying it didn't work? I ran your code on a test file I created and it worked just fine for me on an NTFS volume. I verified changed time looking at properties window for the file in Windows Explorer. – Samuel Neff Jun 15 '13 at 17:25
  • I checked it in the Windows Explorer too. I think it happened what Grant was saying because when I just looked at it (after some hours of sleep) it actually did change the creation date of the file... Thanks guys! – pandita Jun 16 '13 at 00:25

6 Answers6

43

Actually, each file has three different times:

  1. Creation time
  2. Last access time
  3. Last write time (that's shown in Explorer and other file managers as "File Date")

To modify these times you can use

File.SetCreationTime(path, time);
File.SetLastWriteTime(path, time);
File.SetLastAccessTime(path, time);

respectively.

It seems, that if you want to change file date as it shown in file manager (e.g. Explorer) you should try something like that:

String path = @"changemydate.txt";                
DateTime time = new DateTime(year, month, day, hour, minutes, seconds); 

if (File.Exists(path))
    File.SetLastWriteTime(path, time);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Perfect! It is the correct answer!!! It will show in the Windows Explorer after changed. just remember if the file is not there, you must save the file before – Junior Grão Feb 04 '21 at 18:00
4

I had some trouble with this. This was my code:

    FileInfo fileInfo = new FileInfo(path);

    // do stuff that adds something to the file here

    File.SetAttributes(path, fileInfo.Attributes);
    File.SetLastWriteTime(path, fileInfo.LastWriteTime);

Looks good, does it not? Well, it does not work.

This does work though:

    FileInfo fileInfo = new FileInfo(path);

    // note: We must buffer the current file properties because fileInfo
    //       is transparent and will report the current data!
    FileAttributes attributes = fileInfo.Attributes;
    DateTime lastWriteTime = fileInfo.LastWriteTime;

    // do stuff that adds something to the file here

    File.SetAttributes(path, attributes);
    File.SetLastWriteTime(path, lastWriteTime);

And Visual Studio does not help. If you break on the line that resets the time, the debugger will report the original value you want to write back. So this looks good and leads you to believe you are injecting the right date. It seems VS is not aware of the transparency of the FileInfo object and is reporting cached values.

The documentation for FileInfo states:

When the properties are first retrieved, FileInfo calls the Refresh method and caches information about the file. On subsequent calls, you must call Refresh to get the latest copy of the information.

Well... not quite, apparently. It appears to refresh on its own.

Martin Maat
  • 714
  • 4
  • 23
  • 1
    I still can't get this to work on Win 10, no matter what I do. Specifically `File.SetCreationTime`: – bkwdesign Sep 14 '20 at 20:06
  • 2
    I needed to add this to my loop through the FileInfo[] array: `fileInfo.IsReadOnly = false;` this allowed the other code shown in the answers here to work properly – bkwdesign Sep 14 '20 at 20:23
2

You can use this code sample

string fileName = @"C:\MyPath\MyFile.txt"; 
if (File.Exists(fileName)) 
{       
    DateTime fileTime = DateTime.Now; 
    File.SetCreationTime(fileName, fileTime);         
}
Shyam sundar shah
  • 2,473
  • 1
  • 25
  • 40
2

I've never had trouble with SetCreationTime... but I think you can set it on FileSystemInfo via getter/setter CreationTime. Perhaps that will better handle meta-info caching trouble with SetCreationTime.

For example:

static void SetCreationTime(FileSystemInfo fsi, DateTime creationTime)
{
 fsi.CreationTime = creationTime;
}
Matt Self
  • 19,520
  • 2
  • 32
  • 36
2

Thanks again for everybody's help. I got everything working now and thught I share all the work with all the other beginners like me:

https://github.com/panditarevolution/filestamp

The main code is in /FileStamp/program.cs

It is a small command line utility that allows changing the creation date of a file. I used it as a small beginner's project to teach me some basics about c# and commandline interface. It uses the useful CommandlineParser library available here:

http://commandline.codeplex.com/

pandita
  • 4,739
  • 6
  • 29
  • 51
2

In the current Windows 10 version of Visual Studio Community Basic, the statements

Dim fi As New FileInfo(someFilename)
Dim dtf As Date = CDate(someValidDateString)
fi.CreationTime = dtf
fi.CreationTimeUtc = dtf
fi.LastWriteTime = dtf
fi.LastWriteTimeUtc = dtf
fi.LastAccessTime = dtf
fi.LastAccessTimeUtc = dtf

does not work for filetype EML (and perhaps others). Creation works, the other 2 remain unchanged. I use this procedure to archive my emails in a folder per year and want to be able to order them by name or date modified (as those columns are present by default in Explorer). Here my solution is to rename the file to file+"$", change dates, rename the file back to the original.

Martin
  • 1,430
  • 10
  • 19