Unhandled exception when saving to an open file in silverlight

I am trying to write logic to save a file to a local drive in Silverlight 4.0. For this I use a class SaveFileDialog. It is working fine. But when I try to save a file that is already open for viewing, I get an unhandled exception. Also, the application will work immediately.

A similar problem arose with Silverlight 3. There I got rid of the problem by swallowing an exception, searching the text SaveFileStreamfor exceptions, in the application_unhandledexception event. I thought this would be handled in Silverlight 4, but now it's getting worse. Even a workaround now does not work.

I put a try catch around the logic SaveFileDialog, and IOException(another process uses the file) is safely caught here, but the exception described above is immediately raised.

Any help would be appreciated.

Update: this happens with excel files and does not happen with txt files. I would think that this will happen for all MS Office files.

Reporting a problem in the official forum

+3
source share
1 answer

May I ask you how you save the file? Is the stream red, closed, and positioned correctly?

As an example (note: there are many alternatives to do this):

using (Stream stream = new IsolatedStorageFileStream("somefilename.ext", FileMode.Create, FileAccess.Write, IsolatedStorageFile.GetUserStoreForApplication()))
{
    // Use the stream normally in a TextWriter
    using (TextWriter writer = new StreamWriter(stream, Encoding.UTF8))
    {
        writer.Flush();
        writer.Close();
    }

    stream.Close();
}

I hope this helps :-)

+1
source

All Articles