[Silverlight 2, C#] Writing to and Reading from an IsoStorage File at the Same Time!
As I’ve mentioned in past posts, I have augmented the Silverlight Unit Test Framework to log errors it finds into a file in IsoStorage. This file is then read by a python script and results are sent via email if need be.
I ran into a problem today where my python script was throwing an exception on me, stating “Permission Denied” on the result file. I was a little confused, and when I tried to open the file myself [note that this was while my test was still running] I was given the alert:
“The process cannot access the file because it is being used by another process”
Turns out that when you open a file [either by File.Open, or by opening a new IsolatedStorageFileStream] you can set a FileShare flag which will indicate if other processes are allowed to read and/or write to the file at the same time.
private static IsolatedStorageFileStream _resultStream;
private static StreamWriter _resultStreamWriter;
private void OpenResultFile()
{
// Create an isolated storage file [make sure to set access to read so that other processes can read this file while it's open for writing].
IsolatedStorageFile resultStore =
IsolatedStorageFile.GetUserStoreForApplication();
// Here I open up my file with ReadWrite access for my test app, and then set Read access for all other processes.
_resultStream = new IsolatedStorageFileStream("TestResults.txt",
FileMode.Create, FileAccess.ReadWrite, FileShare.Read,
resultStore);
_resultStreamWriter = new StreamWriter(_resultStream);
}
I had been using a different overload of the constructor which did NOT set this flag and so when other processes [namely my python script] was trying to read the file when it was open during the test execution, I would get a permission error.
Tags: isolated storage

It’s grate how you post problems and solutions. I think it’s the best way to teach / learn. Also what plug in are you using for you code highlighting on the blog. I don’t think I have ever seen one that dose word wrap. I like the site, keep it going!
Thank you for the kind words
The plug in I use is called “SyntaxHighlighter Evolved”, it’s really handy and easy to use.
http://wordpress.org/extend/plugins/syntaxhighlighter/