c# - C#中的文件创建时间

标签 c# .net .net-4.5

我需要了解文件的创建时间 - 我已尝试使用:

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

var creationTime = File.GetCreationTimeUtc(FilePath);

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

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

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

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

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

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

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

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

非常感谢任何帮助。

最佳答案

您需要使用 Refresh :

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.

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

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

关于c# - C#中的文件创建时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18339658/

相关文章:

c# - 定义循环上限的异或魔法有什么意义?

.net - 我们应该如何为 NHibernate 实体实现 Equals 和 GetHashCode

c# - .Net 4.5 中的异步 HttpClient 是密集负载应用程序的糟糕选择吗?

.net-4.5 - 从 .Net 4.5 控制台应用程序对 AD FS 进行身份验证

java - Java 无法理解 C# 字节数组

c# - 比较两个通用列表差异的最快方法

c# - 为什么我的 XAML 交互行为在用户控件中不起作用

c# - 如何通过 LINQ 展平树?

asp.net-mvc-4 - 使用 Entity Framework 5 的工作单元和通用存储库

c# - 如何在微软新的Visual Studio Code中编译c#?