File used by another process

<>

This question already has an answer here:

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...


The only option here is to catch the generated exception and handle it appropriately. See these other two SO threads for full discussions:


You must to close your file after using it on your code:

FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read); //file is opened
//you use the file here 
file.Close(); //then you must to close the file

You simply have to try the open with the sharing access you would like, and handle the error that is thrown.

Note: Sometimes the file won't open simply because the file sharing rights you specified conflicts with the file sharing rights that someone already opened the file with.

Please see my answer here for more information.


Try putting the open command in a try-catch statement. If the file is being used IOException will be thrown.


Try to Rename the file if you are able to rename then it is confirm that no other process is accessing that file. Finally make sure that you have renamed the same file in the same folder.


Need Your Help

Call Go functions from C

c shared-libraries go dynamic-linking

I am trying to create a static object written in Go to interface with a C program (say, a kernel module or something).

excel vba freeze pane without select

excel-vba vba excel

I have a VBA script in Excel that freezes the panes of an Excel worksheet, but I'm curious to see if this is possible without first selecting a range. Here's by code now which freezes rows 1 throu...