tags:

views:

70

answers:

2

Hello!

I have been using CopyFileEx since the beginning of times and it has been working great. If the file is not locked, it will copy the file. If the file is locked, it will not and GetLastError will report the error. All fine and dandy.

However, there are some files that are partially locked, PST outlook files being the best example. In that case, CopyFileEx begins to copy, and when it hits the part of the file that is locked, it will hang and never return back (on Windows 7, maybe on other systems too).

Is there any way to avoid this problem?

+2  A: 

You could do the copying yourself, starting with a call to LockFileEx. It has a flag to fail immediately if it can't lock what you've asked it to.

Jerry Coffin
+1  A: 

There is a way to avoid it, but there is no easy solution. Please see my answer here.

Brian R. Bondy
Yes I know and use VSC via API, but my question is about a direct copy.
Lobuno
The open file drivers I mentioned can do that work for you. And the other part of my answer that applies is that there is no easy way other than using these drivers.
Brian R. Bondy
@Lobuno: You could code your own file system filter driver, but that's probably out of the scope of your needs and probably a multi-year project alone.
Brian R. Bondy
Yes, that's overkill... Actually my program uses VSC, but for those users that for one reason or another don't have that capability I just try a direct copy. It's not so important to me that if file gets copied or not. What's it's important is that the function fails or not, but that it doesn't hang my application.
Lobuno