2

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?

Community
  • 1
  • 1
Camper
  • 145
  • 1
  • 7

1 Answers1

0

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.

ctacke
  • 66,480
  • 18
  • 94
  • 155
  • I'm a little confused by your comment about the linked answer being incorrect - the answer to the linked question is also by you. Was there a different answer accepted before? – cost Mar 24 '16 at 00:53
  • I don't think so. Just looking at these now, the other answer is definitely wrong. The API takes in a HANDLE as the first parameter, not a string path. The answer here is correct, the other one (even though I posted it) is wrong. – ctacke Mar 25 '16 at 15:16