0

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.

James
  • 1,764
  • 5
  • 31
  • 49
  • 1
    Does this answer your question? [Reading a file used by another process](https://stackoverflow.com/questions/9759697/reading-a-file-used-by-another-process) – 41686d6564 stands w. Palestine Aug 19 '20 at 10:17
  • Pretty much, although didn't turn up in a search.I'd assumed Excel files were doing something at an OS level, making it an exception to general file access. – James Aug 19 '20 at 10:34
  • This question is asked, and answered before: https://stackoverflow.com/questions/9759697/reading-a-file-used-by-another-process – Luuk Aug 19 '20 at 11:01

1 Answers1

0

Oh, just found the answer; Change the last argument to FileShare.ReadWrite

James
  • 1,764
  • 5
  • 31
  • 49