views:

708

answers:

1

I am trying to write a utility that will allow moving files in Windows, and when it finds a file in use, will set that file to be moved on reboot.

It seems that MoveFileEx (http://msdn.microsoft.com/en-us/library/aa365240(VS.85).aspx) is the right call for this, however I cannot figure out what error code I'm looking for from GetLastError (http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx) to see that the file was in use.

I want the utility to fail when there is an actual permissions problem. Is there anyway to differentiate a you-can't-write-there and a in-use overwrite error?

Also, if I have the files I am moving in the user's temporary folder, will they get deleted before the delayed rename?

+8  A: 

You have to call CreateFile first to see if the file is in use.

To see if the file is in use:

If you get a valid file handle then you know the file does not have conflicting sharing permissions with a process that already has this file open.

If you specify no sharing access (0 to the dwShareMode parameter of the CreateFile call), then you will not get a file handle if any other process is currently using that file in any way. GetLastError in this case would return: ERROR_SHARING_VIOLATION (32)


To see if there is a security problem with accessing the file:

To see if there is a permissions problem accessing that file, the CreateFile call will also fail but with a different GetLastError. You will get: ERROR_ACCESS_DENIED (5)

Brian R. Bondy