private static void SetLastWriteTimeHelper(string path, long lastWriteTime) { #if DotNetCoreClrLinux throw new NotSupportedException("This operation is not supported on Linux. Please use File.SetLastWriteTime API"); #else using (var pin = new PinCollection()) { // Default values indicate "no change". Use defaults. var basicInfo = new Kernel32Types.FILE_BASIC_INFO { CreationTime = -1, LastAccessTime = -1, LastWriteTime = lastWriteTime, ChangeTime = -1, FileAttributes = 0 }; // To update the file last written time, it needs to be opened up with Write access. using (FileStream file = FabricFile.Open(path, FileMode.Open, FileAccess.ReadWrite)) { var success = SetFileInformationByHandle( file.SafeFileHandle, Kernel32Types.FILE_INFO_BY_HANDLE_CLASS.FileBasicInfo, ref basicInfo, Marshal.SizeOf(typeof(Kernel32Types.FILE_BASIC_INFO))); if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } } #endif }
public static extern bool SetFileInformationByHandle(SafeHandle hFile, Kernel32Types.FILE_INFO_BY_HANDLE_CLASS FileInformationClass, ref Kernel32Types.FILE_BASIC_INFO FileBasicInformation, int dwBufferSize);
public static bool SetFileInformationByHandle(SafeHandle hFile, Kernel32Types.FILE_INFO_BY_HANDLE_CLASS FileInformationClass, ref Kernel32Types.FILE_BASIC_INFO lpFileInformation, int dwBufferSize) { return(true); }