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

发布于 2024-08-18 11:14:27 字数 1035 浏览 1 评论 0原文

我确实有一个非常奇怪的问题!我想知道问题是否出在框架、操作系统中,或者也许只是我,误解了事情......

我有一个文件,可能是很久以前创建的,我使用该文件,然后我想将其存档,通过改变它的名字。然后我想创建一个新文件,其名称与旧文件重命名之前的名称相同。够简单的!

真正让我困惑的问题是,新创建的文件的“创建”时间戳是错误的!这是一个问题,因为我想用时间戳来确定何时存档和创建新文件。

我创建了一个非常小的样本来显示问题。为了使示例正常工作,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();
}

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌辣 2024-08-25 11:14:27

这是一个可以追溯到 Windows 旧时代的神秘“功能”的结果。核心详细信息如下:

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

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

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

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.

顾北清歌寒 2024-08-25 11:14:27

我最近遇到了问题中描述的相同问题。在我们的例子中,如果我们的日志文件超过一周,我们将删除它并开始一个新的。然而,自 2008 年以来,它一直保持相同的创建日期。

这里的一个答案描述了重命名旧文件,然后创建一个新文件,希望能够选择正确的创建日期。然而,这对我们来说并不成功,它使旧的日期保持不变。

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

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.

倾城月光淡如水﹏ 2024-08-25 11:14:27

File.SetCreationTime("文件", DateTime.Now);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文