您可以通過在Kernel32.dll中使用P/Invoke調用來實現它。
This Powershell script來自MS TechNet的實現它,並明確指出FileSystemWatcher
的事件不會觸發。
我簡要介紹了腳本,代碼非常簡單,可以很容易地複製到C#項目中。
聲明:
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetFileTime(IntPtr hFile, ref long lpCreationTime, ref long lpLastAccessTime, ref long lpLastWriteTime);
腳本使用SetFileTime
寫入之前,鎖定該文件的時間。
private const int64 fileTimeUnchanged = 0xFFFFFFFF;
這個常數被傳遞作爲參考的lpCreationTime,lpLastAccessTime和lpLastWriteTime方法:
// assuming fileStreamHandle is an IntPtr with the handle of the opened filestream
SetFileTime(fileStreamHandle, ref fileTimeUnchanged, ref fileTimeUnchanged, ref fileTimeUnchanged);
// Write to the file and close the stream
你真的真的真的想要嗎?你實際上是在寫文件的,但是你可以暫停FileSystemWatcher(這已經不可靠了),所以你不必瞎寫假時間。 – JustAnotherUserYouMayKnow 2013-04-09 14:04:15
我不這麼認爲!除非你更換底層驅動程序:P :)! – 2013-04-09 14:05:31
@JustAnotherUserYouMayKnow:'FileSystemWatcher'不一定在我的控制之下:/ – tanascius 2013-04-09 14:20:44