Trouble with File.Copy

  • Thread starter Thread starter Jerry J
  • Start date Start date
J

Jerry J

I am using File.Copy(sSrcFile, sDestPath, true); to copy a file to a folder.
This usually works, but somtimes I get this error "Access to 'my destination
file path is denied."

I realize that there must be some process connected to the file that causes
this problem, however, I can always manually copy the file without a problem.

Can someone tell me what I can do to force the file copy to occur?
 
Jerry said:
I am using File.Copy(sSrcFile, sDestPath, true); to copy a file to a folder.
This usually works, but somtimes I get this error "Access to 'my destination
file path is denied."

I realize that there must be some process connected to the file that causes
this problem, however, I can always manually copy the file without a problem.

Can someone tell me what I can do to force the file copy to occur?
You can't "force" your way through an "access denied", that would defeat the
purpose.

The most likely cause of this is that another process (or possibly even your
*own* process) is still holding an exclusive handle to the file open. You
might just get lucky when you copy the file manually.

The only way to circumvent this would be to forcibly close the open handle,
but this is something that should be reserved as a last resort task for
specialized tools (like Unlocker, http://ccollomb.free.fr/unlocker/).

As an easy workaround, you can try the copy a few times with a small delay
between failed copy attempts. If the other process is only opening the file
briefly (as your successful manual copying seems to suggest) this should
work. Alternatively, do a more thorough investigation into who is opening
the file for what purpose, and whether they're closing it properly. You may
need to explicitly cooperate.
 
Back
Top