I am trying to writ开发者_如何学编程e 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 :-)
精彩评论