C#中的文件创建时间 [英] File creation time in C#

查看:31
本文介绍了C#中的文件创建时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取文件创建时间 - 我尝试使用:

I need to get when a file was created - I have tried using:

FileInfo fi = new FileInfo(FilePath);
var creationTime = fi.CreationTimeUtc;

var creationTime = File.GetCreationTimeUtc(FilePath);

这两种方法通常都会返回错误的创建时间 - 我猜它被缓存在某处.

Both methods generally return the wrong creation time - I guess it is being cached somewhere.

文件被删除并使用相同的名称重新创建,我需要知道它何时/是否已重新创建(通过检查创建的日期/时间是否已更改)-我计划通过查看来执行此操作文件创建时间已更改,但我发现这是不准确的.

The file is deleted and re-created with the same name and I need to know when/if it has been re-created (by checking if the created date/time has changed) - I had planned to do this by seeing it the file creation time had changed but I have found this to be inaccurate.

我正在使用 Win 7,如果我检查文件资源管理器,它会正确显示新文件的创建时间.

I'm working on Win 7 and if I check File Explorer it shows the new file creation time correctly.

我也尝试过使用 FileSystemWatcher,但它并不完全适用于我的用例.例如.如果我的程序没有运行,则 FileSystemWatcher 没有运行,所以当我的程序再次启动时,我不知道文件是否已被删除和重新创建.

I have also tried using the FileSystemWatcher but it doesn't entirely work for my use case. E.g. if my program is not running, the FileSystemWatcher is not running, so when my program starts up again I don't know if the file has been deleted and recreated or not.

我看过 MSDN http://msdn.microsoft.com/en-us/library/system.io.file.getcreationtime.aspx 它说:

I've seen MSDN http://msdn.microsoft.com/en-us/library/system.io.file.getcreationtime.aspx where it says:

此方法可能返回不准确的值,因为它使用的本机函数的值可能不会被操作系统持续更新.

This method may return an inaccurate value, because it uses native functions whose values may not be continuously updated by the operating system.

但我也尝试过使用他们的替代建议并在创建新文件后设置 SetCreationDate 但我也发现这不起作用.请参阅下面的测试:

But I have also tried using their alternative suggestion and setting the SetCreationDate after creating a new file but I also find that this doesn't work. See test below:

    [Test]
    public void FileDateTimeCreatedTest()
    {
        var binPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        var fullFilePath = Path.Combine(binPath, "Resources", "FileCreatedDatetimeTest.txt");
        var fullFilePathUri = new Uri(fullFilePath);


        var dateFormatted = "2013-08-17T15:31:29.0000000Z"; // this is a UTC string
        DateTime expectedResult = DateTime.MinValue;
        if (DateTime.TryParseExact(dateFormatted, "o", CultureInfo.InvariantCulture,
            DateTimeStyles.AssumeUniversal, out expectedResult)) // we expect the saved datetime to be in UTC.
        {

        }

        File.Create(fullFilePathUri.LocalPath);
        Thread.Sleep(1000); // give the file creation a chance to release any lock

        File.SetCreationTimeUtc(fullFilePathUri.LocalPath, expectedResult); // physically check what time this puts on the file. It should get the local time 16:31:29 local
        Thread.Sleep(2000);
        var actualUtcTimeFromFile = File.GetCreationTimeUtc(fullFilePathUri.LocalPath);

        Assert.AreEqual(expectedResult.ToUniversalTime(), actualUtcTimeFromFile.ToUniversalTime());

        // clean up
        if (File.Exists(fullFilePathUri.LocalPath))
            File.Delete(fullFilePathUri.LocalPath);
    }

非常感谢任何帮助.

推荐答案

您需要使用 刷新:

You need to use Refresh:

FileSystemInfo.Refresh 从当前文件中获取文件快照文件系统.即使刷新也无法纠正底层文件系统文件系统返回不正确或过时的信息.这个可以发生在 Windows 98 等平台上.

FileSystemInfo.Refresh takes a snapshot of the file from the current file system. Refresh cannot correct the underlying file system even if the file system returns incorrect or outdated information. This can happen on platforms such as Windows 98.

在尝试获取属性之前必须调用 Refresh信息,否则信息将过时.

Calls must be made to Refresh before attempting to get the attribute information, or the information will be outdated.

来自 MSDN 的关键位表明它拍摄快照属性信息..将过时.

The key bits from MSDN indicate that it takes a snapshot and attribute information..will be outdated.

这篇关于C#中的文件创建时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆