GetLastWriteTime returning 12/31/1600 7:00:00 PM

前端 未结 6 1723
栀梦
栀梦 2020-12-02 01:50

I am using the following code to write the Date Modified time of a Directory to a label

string selectedPath = comboBox1.SelectedItem.ToString();
DateTime las         


        
6条回答
  •  春和景丽
    2020-12-02 02:26

    An easy way to test for file not found with the result of GetLastWriteTime()/GetLastWriteTimeUtc() without hardcoding the sentinel epoch date/times that are used to indicate a file/dir not found condition, is as follows:

    // ##### Local file time version #####
    DateTime fileTimeEpochLocal=DateTime.FromFileTime(0);
    // Use File.GetLastWriteTime(pathname) for files
    // and Directory.GetLastWriteTime(pathname) for directories
    DateTime lastWriteTime=Directory.GetLastWriteTime(selectedPath); 
    
    // Check for a valid last write time
    if (lastWriteTime!=fileTimeEpochLocal) // File found
        DoSomethingWith(selectedPath,lastWriteTime);
    else // File not found
        HandleFileNotFound(selectedPath);
    
    // ##### UTC file time version #####
    DateTime fileTimeEpochUtc=DateTime.FromFileTimeUtc(0);
    // Use File.GetLastWriteTimeUtc(pathname) for files
    // and Directory.GetLastWriteTimeUtc(pathname) for directories
    DateTime lastWriteTimeUtc=Directory.GetLastWriteTimeUtc(selectedPath);
    
    // Check for a valid last write time
    if (lastWriteTimeUtc!=fileTimeEpochUtc) // File found
        DoSomethingWith(selectedPath,lastWriteTimeUtc);
    else // File not found
        HandleFileNotFound(selectedPath);
    

提交回复
热议问题