How do I open a file that is opened in another application - c#

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?

You could try passing FileShare.ReadWrite when opening the file:
using (var stream = new FileStream(
#"d:\myfile.xls",
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
}

How are you trying to open the file?
If you are trying to open it for read/write then you'll get the exception. If you are trying to open it for read only then you should be OK.
var file = File.Open("file.xls", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
This will only work if Excel has opened the file with FileShare.Read set to allow other applications (i.e. your's) to have access to the file. If this isn't set then Excel will have opened the file with exclusive access. Note: I don't think this is the case as you can open an Excel file (in Excel) for read if someone else has it open for edit.
UPDATE - OK I didn't test this properly until after darin's comments. You need the FileShare.ReadWrite flag despite the help indicating that it's for subsequent file openers. Not even FileShare.Read is good enough, which I find even odder.

SpreadsheetGear for .NET can read workbooks while Excel has them open. Here is the code we use to do it (note that we lock the entire file after opening to keep Excel or any other app from writing while we are in the middle of reading):
stream = new System.IO.FileStream(path,
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.ReadWrite,
SG.CompoundDocumentIO.Storage.OpenBufferLength);
try
{
stream.Lock(0, stream.Length);
}
catch
{
// .NET 1.1 requires cast to IDisposable
((IDisposable)stream).Dispose();
throw;
}
Disclaimer: I own SpreadsheetGear LLC

Try making a copy of the already opened file, read it and discard it. In order to check if the file is already opened, try reading and handle the exception by doing the copy, read, discard.

Related

Open an Excel file in C# as read-only whilst file is open in Excel

I'm writing a application which converts data from an excel file into an XML file for another application. You make some changes to the excel file, save them, load them in my C# forms application as readonly and then save the resultant xml. Only problem is, this is a very iterative process and at the moment my application throws an exception if the file is open in excel. So to get it to work, you have to shut down excel (or close file), run application, then reopen excel.
My code is this;
using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
which causes a IO Exception.
Oh, just found the answer; Change the last argument to FileShare.ReadWrite

Opening and reading a file which is open in Excel

It is possible to use a FileShare value of FileShare.ReadWrite to open a file for reading, while it is already open in other programs (e.g. like Excel). e.g.:
using (FileStream fs = new FileStream(#"c:\abd\somefile.xlsx",
FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
// read file, etc.
}
Just wondering if this a good idea. e.g. in worst case, what will happen if the external program is writing to the file, and your code is trying to read it at the same time ?
I have noticed libraries like spreadsheet gear that can read files even when they are open in Excel - are they basically hoping that they will be able to read the whole file into memory before any part of it is changed ?

Why does a TextReader complain when file is opened by another process?

See simple unit test example below
[Test]
public void TextReaderRequiresWriteAccessToFile()
{
using (var reader = File.OpenText(Path.Combine(Folder, "Texts.txt")))
{
}
}
This test fails when I open the text file in an editor. I'd expect that the file would be opened with read privileges by File.OpenText.
System.IO.IOException : The process cannot access the file
'C:\*a location to a folder*\Texts.txt'
because it is being used by another process.
Is there a reason why .NET requires extra privileges to open a file for read via this API?
Oh, but it does only require read privileges.
The problem is that the editor prevents even read access (in .NET, that would be FileShare.None - the default for write access). If you opened the file using OpenText in both cases, rather than using the editor, it would work just fine. So you probably want an editor that doesn't lock the file (for example, Visual Studio).
It's not a problem of .NET - the editor prohibits you from opening the file for reading altogether. You might want to read up on sharing file access a bit.
When your file is open in a text editor, it has a handle open to it. That means, you need to explicitly open your file with FileShare.Read. This way, you'll get Read access while the file is already open in Read/Write mode somewhere else.
using (var reader = File.OpenText(Path.Combine(Folder, "Texts.txt"), FileMode.Open, FileAccess.Read, FileShare.Read))
{
}

How to read open excel file at C#

I want to read already open excel file with C#. I am using this method but it can't read the excel file while the file is open in Microsoft excel.
FileStream stream = File.Open("myfile.xlsx", FileMode.Open, FileAccess.Read);
It gives IOException: The process cannot access the file 'myfile.xlsx' because it is being used by another process.
I hope you understands what I mean. I want to keep excel file open and while file is open at Microsoft excel i want to read it from C#. I am using C# net framework 4.0
You need to open it with FileShare.ReadWrite:
FileStream stream = File.Open("myfile.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
See this answer.
I think you can still copy the file while excel has it open, so you could make a copy of the file and then open that. Just make sure you clean up after yourself when you are done with the copy.
You could use the Interop library to use the already opened instance of Excel.
oExcel == (Excel.Application) System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
You can try the File.Open with a fourth parameter - fileShare.
FileStream stream = File.Open("myfile.xlsx", FileMode.Open, FileAccess.Read, FileShare.Read);
You may need to specify write access also.
To ensure that correct opening and closing of the file please look at using the c# using statements
using (FileStream stream = File.Open("myfile.xlsx", FileMode.Open, FileAccess.Read))
{
}
To open the same file more than once at the same time, it needs to be opened in shared mode.
Hope this may help others.

Exception in opening a file that is already open

I am building an application in C# in which I have to open a CSV file to read data from it. I get an exception when I try to open the CSV file from C# when that file is already open in Excel. The exception says that the process cannot access the file since it is already open. How can I solve this problem and open the file even if it is opened in other application?
Thanks,
Rakesh.
I faced this problem some time back.
You are missing the FileShare parameter. Without specifying that, if you open a file, it will be locked exclusively by your application. But since it's already been opened by Excel (or any other app), you will receive an exception.
You can try using this - I think this will be your best bet -
using (FileStream fs = File.Open(<file-path>, FileMode.Open, FileAccess.Read, FileShare.Read))
This code says: Hello Excel! If you may permit (read, not throw exception), I would like to read the file, though I will not try to own it and I know that you may modify it anytime.
If this throws error, then Excel has denied you even the read access. Too bad then!
All the best.
It is possible but you have to carefully control the file sharing you specify. Most .NET classes default to FileShare.Read, denying another process from writing to the file. But that cannot work if the file is opened by Excel, it already gained write access to it. You cannot deny a right that was already acquired.
To fix the problem, make your code look similar to this:
using (var fs = new FileStream(#"c:\\temp\\test.csv", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
using (var sr = new StreamReader(fs)) {
// Read it...
}
Note the use of FileShare.ReadWrite. I verified this code works while Excel had test.csv opened.
Beware of the potential trouble you'll invite with this, odd things can happen when Excel writes to the file just as you are reading it. You'll likely read garbage, part of old data, part of new, without a good way to diagnose this.
Due to concurrency issues you can not have the option to write to two instances of the same file. It should be possible to open one as read-only this would allow for there to not be a concurrency issue as reading is guaranteed to be thread safe. This article should explain how to do what I proposed
That's not possible.
A file can be opened with different kind of protection. Excel opens the file exclusively, for the purpose of protecting the file from being changed by some other program and then reverted back when Excel saves it.
Excel could have opened the file and allowed reading, but then you could end up in a deadlock situation where two applications have the file open for reading, and neither can save anything back to it.
Another solution, suggested by this answer, is to copy the file to a temporary file and open that.
Use
System.IO.File.Copy(sourcepath, copypath, false);

Categories