在C#中的文件创建时间文件、时间

2023-09-04 09:46:29 作者:一个人寂寞

我需要创建一个文件时得到 - 我曾尝试使用:

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

任何帮助非常AP preciated。

Any help much appreciated.

推荐答案

您需要使用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.

呼叫必须进行刷新试图获取属性之前   信息,或该信息将被过时。

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

从MSDN的关键位表明它的需要一个快照和属性information..will过时的。

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