Unbelievable strange file creation time problem
Asked Answered
F

3

28

I have a very strange problem indeed! I wonder if the problem is in the framework, OS or maybe it's just me, misunderstanding things...

I have a file, which might be created a long time ago, I use the file, and then I want to archive it, by changing it's name. Then I want to create a new file, with the same name as the old file had, before it was renamed. Easy enough!

The problem that really puzzles me, is that the newly created file gets wrong "created"-timestamp! That's a problem since it's that timestamp that I want to use for determing when to archive and create a new file.

I've created a very small sample that shows the problem. For the sample to work, there must be a file 1.txt in the Files folder. Also, the file attribute must also be set back in time (with one of the tools available, I use Nomad.NET).

static void Main(string[] args)
{
    // Create a directory, if doesnt exist.
    string path = Path.GetDirectoryName(Application.ExecutablePath) + "\\Files";
    Directory.CreateDirectory(path);

    // Create/attach to the 1.txt file
    string filename = path + "\\1.txt";
    StreamWriter sw = File.AppendText(filename);
    sw.WriteLine("testing");
    sw.Flush();
    sw.Close();
    // Rename it...
    File.Move(filename, path + "\\2.txt");

    // Create a new 1.txt
    sw = File.AppendText(filename);
    FileInfo fi = new FileInfo(filename);
    // Observe, the old files creation date!!
    Console.WriteLine(String.Format("Date: {0}", fi.CreationTime.Date));

    Console.ReadKey();
}
Fullgrown answered 21/1, 2010 at 12:35 Comment(2)
Finally, I solved it by after the file was created (with the wrong/old timestamp), I changed it "manually" by setting the FileInfo.CreationTime = DateTime.Now.Fullgrown
Related: NTFS file attributes from deleted file applying to new one with the same name and Wrong date created for files in Windows when copying files, if the file has already existed  (on Super User).Gwynethgwynne
S
33

This is the result of an arcane "feature" going way back to the old days of Windows. The core details are here:

Windows NT Contains File System Tunneling Capabilities (Archive)

Basically, this is on purpose. But it's configurable, and an anachronism in most of today's software.

I think you can create a new filename first, then rename old->old.1, then new->old, and it'll "work". I don't remember honestly what we did when we ran into this last a few years back.

Struma answered 21/1, 2010 at 12:57 Comment(3)
Thanks, this is exactly my problem. Never heard of it before. Now I can probably find a workaround by renaming to a temporary random name.Fullgrown
Here an interesting German article File System Tunnel und mein Leid mit "CreatedTime" und Rename -- BTW: exact the problem I faced todayGibrian
There is also this article The apocryphal history of file system tunnelling | The Old New Thing (with a dead link to KB172190 inside)Gibrian
G
9

I recently ran into the same problem described in the question. In our case, if our log file is older than a week, we delete it and start a new one. However, it's been keeping the same date created since 2008.

One answer here describes renaming the old file and then creating a new one, hopefully picking up the proper Creation Date. However, that was unsuccessful for us, it kept the old date still.

What we used was the File.SetCreationTime method, and as its name suggests, it easily let us control the creation date of the file, allowing us to set it to DateTime.Now. The rest of our logic worked correctly afterwards.

Girlie answered 13/6, 2011 at 13:47 Comment(0)
W
2

File.SetCreationTime("file", DateTime.Now);

Wednesday answered 13/3, 2013 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.