简体   繁体   中英

Unhandled exception when saving to an already open file in silverlight

I am trying to write logic for saving a file to local disk in Silverlight 4.0. I am using the SaveFileDialog class for that. It works fine. But when I am trying to save to a file that is already opened for viewing, I am getting an unhandled exception. Also the application crashes immediately.

Similar problem was there with Silverlight 3. There I got rid of the issue by swallowing the exception by searching for some SaveFileStream text in exceptions, in the application_unhandledexception event. I thought this would be handled in Silverlight 4, but it got worse now. Even the workaround is not working now.

I have put try catch around the SaveFileDialog logic and the IOException (another process is using file) is safely caught here, but immediately the exception that I described above is triggered.

Any help would be appreciated.

Update: This happens with excel files and not happening with txt files. I would think this would occur for all MS Office files.

A post about the issue on the official forum

May I ask you how you save the file? Is the Stream flushed, closed and disposed properly?

Like this as an example (note: there are lots of alternative says 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();
}

Hope it is to and help:-)

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