C#. Love it or hate it, it’s definitely a quick, easy way to get stuff done on Windows. When you’re writing tools, sometimes it’s a no-brainer. Recently I found myself writing a tool that had to save an Excel version of a file, and some computers would definitely not have the latest version so I couldn’t just take on the COM Interops. It went mostly smooth except for a few IO errors. For some reason, Excel does not play nice with other files, always requesting an exclusive lock on a file it has open. Notepad got around this somehow, why couldn’t I?


After much annoyance, I turned to the internet to see why I couldn’t just open a file for read only in C# if it was already open in Excel. I’ve already added some neat functionality in the application I’m working on such that you can open our proprietary format in Excel for easier editing. I watch the file with a FileSystemWatcher with the following code:

1
2
3
private FileSystemWatcher fileWatcher = new FileSystemWatcher(path, file);
fileWatcher.Changed += this.fileWatcher_Changed;
fileWatcher.EnableRaisingEvents = true;

Yeah, it’s that easy. There are some other properties I’m playing around with, such as NotifyFilter to narrow down the scope of events I receive and handle, but that’s really all you need. Now, the problem I was having is that when the user saves in Excel I want to capture that data and update my internals. I tried opening the code with File.Open like so:

1
FileStream fileDataStream = File.Open(file, FileMode.Open, FileAccess.Read);

However, I quickly discovered that Excel opens all files in Exclusive mode. I thought opening the file in ReadOnly mode would work.

1
FileStream fileDataStream = File.OpenRead(file);

I, again, was quickly proven wrong. Finally, I found this blog entry that was very enlightening. I guess opening a file in Read Only mode was not enough because OpenRead does not play nicely with other files. You need to manually open the file and pass in some FileShare parameters so that your request to open the file lets the file system (and anyone with the file open) know that they still have Read and Write access. You do that with the following code:

1
FileStream fileDataStream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

Exception avoided! Now I can read those .xls files all day.

  • Share/Bookmark