2017-05-06 131 views
0

我正在嘗試獲取文件的創建日期。我正在使用File.GetCreationTime() method來做到這一點。如果該文件是一個新文件,它似乎工作正常。如果我刪除該文件並重新創建它,它似乎給了我原創的時間。由於該文件已被刪除,它似乎很奇怪,甚至不可能返回文件的原始日期和時間。File.GetCreationTime()返回奇怪的值

我放在一起簡單的控制檯應用程序來演示這個問題:

static void Main(string[] args) 
{ 
    const string fileName = @"C:\Temp\dummy.txt"; 

    File.AppendAllText(fileName, "This is a test"); 
    DateTime creationDate = File.GetCreationTime(fileName); 

    Console.WriteLine(creationDate.ToShortDateString() + " " + creationDate.ToShortTimeString()); 

    System.Threading.Thread.Sleep(120000); 
    File.Delete(fileName); 

    File.AppendAllText(fileName, "This is a test"); 
    creationDate = File.GetCreationTime(fileName); 

    Console.WriteLine(creationDate.ToShortDateString() + " " + creationDate.ToShortTimeString()); 

} 

這個程序創建一個虛擬文件並追加文本This is a test。然後它將創建日期和時間輸出到控制檯屏幕。到現在爲止還挺好。然後睡2分鐘。 2分鐘過後,它會刪除文件並重新創建它。然後,它再次將創建日期和時間打印到控制檯屏幕上。我會除了後者輸出比原來晚2分鐘,但是,它是拉同一確切的日期和時間!我單步執行了該程序,我可以確認它確實從硬盤中刪除了原始文件。

Actual Output 
-------------- 
5/6/2017 10:25 AM 
5/6/2017 10:25 AM 


Expected Output 
---------------- 
5/6/2017 10:25 AM 
5/6/2017 10:27 AM 

有人可以向我解釋這裏發生了什麼,以及如何解決這個問題?

+0

可能是因爲這樣:http://stackoverflow.com/questions/8804342/windows-filesystem-creation-time-of-a-file-doesnt-change-when-while-is-deleted – Crowcoder

回答

0

MSDN頁面

NTFS格式化的驅動器可以高速緩存信息有關文件,如文件的創建時間,對於短時間。因此,如果覆蓋或替換現有文件,則可能需要明確設置文件的創建時間。