4

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?

Bruce McGee
  • 15,076
  • 6
  • 55
  • 70
Lobuno
  • 1,405
  • 1
  • 18
  • 28
  • 1
    I suppose you wouldn't consider "Expunge Outlook from all your systems" a good answer? :-) – Jerry Coffin Feb 24 '10 at 14:12
  • Similar problem here; only when called from .Net code through IO.Copy, the resulting file is deleted. – Clément Mar 05 '11 at 21:29
  • I'm stuck with the same problem CopyFileEx hangs on Windows 7, can't see a reason why? Callback isn't even invoked and caller hasn't returned from CopyFileEx ;) – Bartosz Wójcik Nov 30 '13 at 00:12

2 Answers2

2

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
  • 476,176
  • 80
  • 629
  • 1,111
2

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

Community
  • 1
  • 1
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • Yes I know and use VSC via API, but my question is about a direct copy. – Lobuno Feb 24 '10 at 14:25
  • 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 Feb 24 '10 at 14:41
  • @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 Feb 24 '10 at 14:43
  • 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 Feb 24 '10 at 14:53