Hi,
First, please note that "the process cannot access the file XXX because it is being used by another process" error comes from .NET and the wording is misleading. Instead of "being used by another process", is should rather say "being used by another process or by the current process". It's quite easy to open a file without closing it by mistake - a common cause is a call to File.Create(path)
without a correspnding Close
call.
The GetFileAsync
method opens the file for writing by calling new FileStream(localPath, FileMode.Create, FileAccess.Write, FileShare.Read)
, so if your process (or another process) opened the file for writing without closing it, GetFileAsync
will fail.
Also, GetFileAsync
always closes the file (in finally
block), even if it fails. It's unlikely there would be a bug there, but in order to rule this out, consider using the Stream-based overload of GetFileAsync
method instead - open the file for writing yourself by calling the FileStream
constructor (as shown above), and close the Stream
when the method succeeds or fails. This way, the file will be completely under your control.