Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File used by another process [duplicate]

Tags:

Many a times we get an error, while trying to write file on Windows platform,

"The process cannot access the file 'XXX' because it is being used by another process."

How to check in C#, before writing to file that its not being used by another process?

like image 833
NileshChauhan Avatar asked Mar 09 '09 12:03

NileshChauhan


People also ask

How do you copy a file that is being used by another process?

To create a copy of a file that is read- and/or write-locked by another process on Windows, the simplest (and probably only) solution is to use the Volume Shadow Copy Service (VSS). The Volume Shadow Copy Service is complex and difficult to call from managed code.

Can xcopy copy open files?

No open files XCOPY does not support the Windows Volume Shadow Copy service which effectively allows processes to have access to open files, so it is not useful for backing up live operating system volumes.

How do I duplicate a file in File Manager?

In the File Manager select the file you want to make a copy of. Click the Copy button. A box will pop up, enter the location you want to copy the file to then click the Copy File(s) button. A copy of your file or folder will then be made in your selected location.


1 Answers

You can't, basically - you have to attempt to open the file for writing, and if you get an exception you can't write to it :(

Even if you could do a separate check first, the result would be out of date before you could start writing - you could still end up with the exception later.

It would be nice if the framework provided a TryOpen method, admittedly...

like image 118
Jon Skeet Avatar answered Sep 20 '22 18:09

Jon Skeet