Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

c# - Unbelievable strange file creation time problem

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();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...