问题
I need to set file creation time after I download it using sockets. In CF FileInfo.CreationTime is readonly. I tried to use P/Invoke solution from this topic, but it won't work: I get error 6 (Invalid handle).
According to MSDN first parameter for SetFileTime() must be a handle, while in the topic mentioned it is string, and somebody says that it works for him. What am I doing wrong? If I need to use handle, how can I get this handle in CF and how should I change SetFileTime() declaration?
回答1:
Interesting. The answer you point to is obviously wrong, though it was accepted.
At any rate, you need to pass in a HANDLE (IntPtr or however you'd like to represent it) that is returned from a call to CreateFile - which you'll also need to P/Invoke.
[DllImport("coredll.dll")]
private static extern bool SetFileTime(IntPtr fileHandle,
ref long creationTime,
ref long lastAccessTime,
ref long lastWriteTime);
Be sure to call CloseHandle when you're done.
来源:https://stackoverflow.com/questions/13036165/change-file-creation-time-on-compact-framework