We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

file in use error

This should be simple. When Excel has a file open, and I attempt to open it with: streamIn = new FileStream(strFile,FileMode.Open,FileAccess.Read,FileShare.Read); I get an exception ("...is being used by another process."). My intent is to open it in read access mode only, to avoid this conflict, but I must have something wrong. Please advise. Thanks, Greg

4 Replies

AD Administrator Syncfusion Team October 6, 2005 04:19 AM UTC

Hi Greg, I herewith attach a Sample for your reference. OpenFromStream_1883.zip Please see to it and let me know if you have further questions. Best Regards, Seetha >This should be simple. > >When Excel has a file open, and I attempt to open it with: > >streamIn = new FileStream(strFile,FileMode.Open,FileAccess.Read,FileShare.Read); > >I get an exception ("...is being used by another process."). My intent is to open it in read access mode only, to avoid this conflict, but I must have something wrong. > >Please advise. > >Thanks, >Greg


GR Greg October 7, 2005 08:38 PM UTC

Did you actually test to see if the sample code you threw my way actually opens a file that is currently opened by Excel? If so, please state that in your reply, because the line of code this sample has for creating the FileStream is identical to mine, which does not work when the file is already opened by Excel. So, now that you have wasted more of my time by my having to install this useless sample to compile and run it, I have proven that it also does not work - just as I suspected. I would appreciate some real help. Sorry for being harsh, but not replying is actually better than throwing out crap. -Greg


AD Administrator Syncfusion Team October 10, 2005 05:49 AM UTC

Hi Greg, Sorry for the Inconvenience. We have already logged a feature request regarding this issue, you can track the status of this issue here. http://www.syncfusion.com/support/features/xlsio/Default.aspx?ToDo=view&questId=233 Please let me know if you have further questions. Best Regards, Seetha >Did you actually test to see if the sample code you threw my way actually opens a file that is currently opened by Excel? > >If so, please state that in your reply, because the line of code this sample has for creating the FileStream is identical to mine, which does not work when the file is already opened by Excel. > >So, now that you have wasted more of my time by my having to install this useless sample to compile and run it, I have proven that it also does not work - just as I suspected. > >I would appreciate some real help. Sorry for being harsh, but not replying is actually better than throwing out crap. > >-Greg


AD Administrator Syncfusion Team October 10, 2005 07:04 AM UTC

Hi Greg, I looked into this issue and it seems that MS Excel locks the file when opening it so it is not even to possible to open the filestream in Read-Only mode. However, I came across this workaround which might help in this situation string strFile = "..\\..\\Template\\OpenFromStreamTemplate.xls"; FileStream fs; //Opening a File from a Stream try { fs = new FileStream(strFile, FileMode.Open, FileAccess.Read); } catch(IOException) { //The file is locked so create a copy. File.Copy(strFile,"Copy.xls",true); //Open the copy. fs = new FileStream("Copy.xls", FileMode.Open, FileAccess.Read,FileShare.ReadWrite); } fs.Seek(0, SeekOrigin.Begin); IWorkbook workbook = excelEngine.Excel.Workbooks.Open(fs); //Close the workbook. workbook.Close(); //No exception will be thrown if there are unsaved workbooks. excelEngine.ThrowNotSavedOnDestroy = false; excelEngine.Dispose(); //Delete the temp file fs.Close(); File.Delete("Copy.xls"); I have also requested the development team to check if there is a more elegant solution to opening a locked file in readonly mode. Sorry for the inconvenience. Thanks, Stephen.

Loader.
Live Chat Icon For mobile
Up arrow icon