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.
我有一个winforms应用程序,它加载excel文件进行分析。目前,为了打开excel文件,文件不能在excel中打开,否则当我尝试加载文件时,会抛出FileIOException。
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.
我想做的是允许我的应用程序在这个文件中读取,即使它是在excel中打开的,而不是强迫用户先关闭工作表。注意,所涉及的应用程序只需要读取文件,而不必写入文件。
Is this possible?
这是可能的吗?
4 个解决方案
#1
27
You could try passing FileShare.ReadWrite when opening the file:
您可以尝试传递FileShare。打开文件时请阅读:
using (var stream = new FileStream(
@"d:\myfile.xls",
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
}
#2
6
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.
只有当Excel用FileShare打开文件时,才有效。读取设置以允许其他应用程序(例如您的应用程序)访问文件。如果没有设置,那么Excel将打开具有独占访问权限的文件。注意:我不认为这是一个例子,因为你可以打开一个Excel文件(在Excel中),如果别人打开它来编辑。
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.
更新-好的,我在达林的评论之后才对它进行了正确的测试。你需要的文件共享。ReadWrite标志,尽管帮助表明它是用于后续的文件打开程序。没有文件共享。读书就够好了,我觉得更奇怪。
#3
1
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):
net的电子表格工具可以阅读工作簿,而Excel可以打开工作簿。这是我们用来做这个的代码(注意,我们在打开后锁定整个文件,以防止Excel或其他任何应用程序在阅读过程中书写):
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
免责声明:我拥有SpreadsheetGear LLC公司
#4
0
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.
尝试复制已经打开的文件,读取它并丢弃它。为了检查文件是否已经打开,请尝试读取并通过复制、读取、丢弃来处理异常。
#1
27
You could try passing FileShare.ReadWrite when opening the file:
您可以尝试传递FileShare。打开文件时请阅读:
using (var stream = new FileStream(
@"d:\myfile.xls",
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
}
#2
6
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.
只有当Excel用FileShare打开文件时,才有效。读取设置以允许其他应用程序(例如您的应用程序)访问文件。如果没有设置,那么Excel将打开具有独占访问权限的文件。注意:我不认为这是一个例子,因为你可以打开一个Excel文件(在Excel中),如果别人打开它来编辑。
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.
更新-好的,我在达林的评论之后才对它进行了正确的测试。你需要的文件共享。ReadWrite标志,尽管帮助表明它是用于后续的文件打开程序。没有文件共享。读书就够好了,我觉得更奇怪。
#3
1
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):
net的电子表格工具可以阅读工作簿,而Excel可以打开工作簿。这是我们用来做这个的代码(注意,我们在打开后锁定整个文件,以防止Excel或其他任何应用程序在阅读过程中书写):
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
免责声明:我拥有SpreadsheetGear LLC公司
#4
0
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.
尝试复制已经打开的文件,读取它并丢弃它。为了检查文件是否已经打开,请尝试读取并通过复制、读取、丢弃来处理异常。