windows 令人难以置信的奇怪文件创建时间问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2109152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:48:36  来源:igfitidea点击:

Unbelievable strange file creation time problem

c#.netwindowsfilesystems

提问by kaze

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).

我创建了一个非常小的示例来显示问题。要使示例工作,Files 文件夹中必须有一个文件 1.txt。此外,还必须及时设置文件属性(使用可用工具之一,我使用 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();
    }

回答by Joe

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

这是一种可以追溯到 Windows 旧时代的神秘“功能”的结果。核心细节在这里:

Windows NT Contains File System Tunneling Capabilities(Archive)

Windows NT 包含文件系统隧道功能存档

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

基本上,这是故意的。但它是可配置的,并且在当今的大多数软件中都是不合时宜的。

I thinkyou 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.

认为你可以先创建一个新的文件名,然后重命名 old->old.1,然后是 new->old,它就会“工作”。老实说,我不记得几年前我们遇到这种情况时我们做了什么。

回答by Aaron

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.

我最近遇到了问题中描述的相同问题。在我们的例子中,如果我们的日志文件超过一周,我们会删除它并开始一个新的。但是,它一直保持自 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.

我们使用的是 File.SetCreationTime 方法,顾名思义,它可以让我们轻松控制文件的创建日期,允许我们将其设置为 DateTime.Now。之后我们的其余逻辑工作正常。

回答by franckspike

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

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