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 ?
Related
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
Is there any equivalent of FileShare = ReadWrite while writing from Microsoft Excel using VBA code.
I have a file watcher implemented in a C# application, to start reading the csv file as soon as it's created (I've tried time delay of 1.5 seconds). But sometimes, due to network latency, or volume of data - Excel is still writing to the file, while my application starts reading it.
I open CSV using following code - But still get Acces violation error " “in use by another process” exception" error
using (var csv = new FileStream(csvFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Thanks
I have a Python service spitting out logs to text files. It rotates them every ~400KB. So the Python service opens up a handle on the file, let's call it app.log. It then writes content to the file every now and again flushes it to the disk. When it reaches a certain size, it closes it's handle, and move it to app.log.1 and starts a new handle on app.log.
So I can't change this service, but I have a C# application that will read those logs. I ran into 3 scenarios:
If I just try to read those those logs using new FileStream(path, FileMode.Open);, it won't allow me as the Python service has an handle on it.
If I try to open it using new FileStream(path, FileMode.Open, FileAccess.Read);, this allows me to read it, but if the service tries to rotate the log, it won't be able to as my C# application now has a handle on the file.
And if I try to open the file using new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete);, my Python service won't fail on deleting the file, but it will fail creating a new handle on app.log as the C# application would still have a handle on it.
The only solution which I'm aware of would be using Windows Shadow Copy (VSS) to create a snapshot of the logs and then read that snapshot but this would be quite expensive as we need to query the logs at every 5 minutes.
Also, I'm not interested in reading the rotated logs, app.log.1, app.log.2 etc.
Logging to text files under Windows seems to be a pain what with all the locking/handles. Does anyone have any suggestion?
You should be able to open your file as Dmitry Popov suggested in his answer as below and not affect Python writing to it, however it depends upon what locks the Python application holds on the file, it can lock you out completely and there is nothing you to do to prevent that without hacking Windows.
FileSream fs = File.Open(#"c:\Test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)
The FileStream object created in this way will still be connected to the same file after an operating system file move operation has been performed on it to rename it.
So lets assume your python app opens a file called Test.log and starts writing to it. You can read any data written to it (after python flushes its buffer) using the file stream returned from the line above. The python application can close and reopen the file as often as it wants writing each time and the reading application will remain connected to it. When the python application issues a File Move operation to rename the file to Test1.log, the file stream returned above will still be connected to the file which is now called Test1.log so you can continue reading to the end of the file before starting the new log file if that is what you want. There is one caveat to this. The Python application needs to use a Move/Rename operation rather than copying the file to a new one and deleting the old one, I'd be surprised if that is what it does though.
There is a possibility that your reading application will reach the end of the file before your writing application has finished reading from it. In this case fs.Read will keep returning 0 after a timeout until the writing application opens the file and writes some more. You can make the time out very long / infinite if you want.
As you don't want to read to the end of one file before starting the new one you could just close and reopen the file at regular intervals. The log file without the numeric suffix should always be the most recent.
If however you want your reading application to read to the end of one log file before starting at the beginning of the next one you will need to work out when the writing application has finished writing to the log file. Also it needs to find out what the file is now called so it can read n-1 next. Is there some marker written by the python application that you could look for to denote the end of a file? Does it write 'End Of Log' or something similar?
Be warned also that there are going to be short periods of time when LogFile n-1 does not exist. This is because if you have log files 0, 1, 2 and 3 it needs to make log file 3 into log file 4 before it can make log file 2 into log file 3. While it is doing this there will be a short period of time when you have log files 0, 1, 2, 4 and no 3.
Personally I would find the developer that wrote the logging for your Python application give him/her the evil eye for causing this headache in the first place. What is wrong with having the most recent log file have the largest number?
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
//Do works
}
C# thread don't lock the file in this case, your Python script can write and close the file to create another one without deadlock.
You can combine FileShare flags:
FileShare.Write | FileShare.Delete
Here's a demo:
using (var cSharp = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Write | FileShare.Delete))
{
// The Python service will be able to change and to rename the file:
using (var python = new FileStream(filename, FileMode.Open, FileAccess.Write, FileShare.Read))
{
}
File.Move(filename, newFilename);
}
You will have to deal with concurrency. You can use FileSystemWatcher to monitor file changes.
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.
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.