/// <summary> /// Obtain the low-level information of the file stream specified. /// </summary> public static void GetLowLevelFileInfo(FileStream stream, out UInt64 fileID, out UInt64 fileSize, out DateTime fileDate) { Syscalls.BY_HANDLE_FILE_INFORMATION bhfi; Syscalls.GetFileInformationByHandle(stream.SafeFileHandle.DangerousGetHandle(), out bhfi); fileID = (bhfi.FileIndexHigh << 32) + bhfi.FileIndexLow; fileSize = (bhfi.FileSizeHigh << 32) + bhfi.FileSizeLow; fileDate = DateTime.FromFileTime((Int64)(((UInt64)bhfi.LastWriteTime.dwHighDateTime << 32) + (UInt64)bhfi.LastWriteTime.dwLowDateTime)); }
/// <summary> /// Return the last modification date of a given file system path. /// If the path leads to a directory, return the creation time of the directory. /// </summary> /// <param name="fullPath"></param> /// <returns></returns> public static DateTime GetLastModificationDate(string fullPath) { FileStream stream = null; try { if (File.Exists(fullPath)) { Syscalls.BY_HANDLE_FILE_INFORMATION bhfi; stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read); Syscalls.GetFileInformationByHandle(stream.SafeFileHandle.DangerousGetHandle(), out bhfi); DateTime observedDate = DateTime.FromFileTime((Int64)(((UInt64)bhfi.LastWriteTime.dwHighDateTime << 32) + (UInt64)bhfi.LastWriteTime.dwLowDateTime)); return(observedDate); } else if (Directory.Exists(fullPath)) { return(new DirectoryInfo(fullPath).CreationTime.ToLocalTime()); } else { return(DateTime.MinValue); } } catch (IOException) { // If we can't open the file directly, try going with the fileinfo method. try { return(new FileInfo(fullPath).LastWriteTime.ToLocalTime()); } catch (Exception) { return(DateTime.MinValue); } } catch (Exception) { return(DateTime.MinValue); } finally { if (stream != null) { stream.Close(); stream = null; } } }
public static UInt64 GetFileSize(string fullPath) { FileStream stream = null; try { Syscalls.BY_HANDLE_FILE_INFORMATION bhfi; stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read); Syscalls.GetFileInformationByHandle(stream.SafeFileHandle.DangerousGetHandle(), out bhfi); return(((UInt64)bhfi.FileSizeHigh << 32) + bhfi.FileSizeLow); } catch (IOException) { // If we can't open the file directly, try going with the fileinfo method. try { return((UInt64) new FileInfo(fullPath).Length); } catch (Exception) { return(0); } } catch (Exception) { return(0); } finally { if (stream != null) { stream.Close(); stream = null; } } }