I have an winforms application that loads in excel files for analysis. Currently, in order to open the excel file the file must not be already open in excel otherwise a FileIOException is thrown when I try and load in the file.
What I would like to do is allow my application to read in the file even if it is open in excel rather than forcing the user to close down the worksheet first. Note that the application in question only needs to read the file, not write to it.
Is this possible?
The error “File is open in another program” occurs when you perform an operation on a file but because the file is being accessed by another program, you are not able to execute tasks or operations on it.
To see the open files for a process, select a process from the list, select the View->Lower Panel View->Handles menu option. All of the handles of type "File" are the open files. Also, a great way to find which application has a file open is by using the Find->Handle or DLL menu option.
Close Programs Running in the Background The quickest way to see what programs are running on your computer is to launch the Task Manager. Then click on the Processes tab. Right-click on the program that's using that file and select End Task to close it.
You could try passing FileShare.ReadWrite when opening the file:
using (var stream = new FileStream( @"d:\myfile.xls", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With