简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM