开发者

Silverlight SaveFileStream Silently Fails

I have a little piece of code that seems to be a textbook example of saving files from Silverlight 4.0, but it doesn't seem to work.

The following snippet comes from a button click handler:

var saveDialog = new SaveFileDialog() { Filter = "All Files(*.*)|*.*" };

if (saveDialog.ShowDialog() == true)
{
    using (var stream = saveDialog.OpenFile())
    using (var writer = new StreamWriter(stream))
    {
        writer.WriteLine("Hello, World!");开发者_如何学JAVA
        writer.Flush();
        writer.Close();
    }
}

I've tried saving a file to many different locations, all with the same behavior:

  • The SaveFileDialog appears to behave normally.
  • The SaveFileStream appears (from the debugger) to behave normally.
  • After the call to writer.Flush(), the BaseStream advances to position 15.
  • No exception is thrown.
  • After the block executes, I cannot find the file using Windows Explorer.

It seems to me that the code is too simple to fail under normal circumstances. So that leads to my question: what is amiss with my circumstances? Why is it that the save appears to complete successfully, but the file is nowhere to be found? Security settings? The code itself? I'm at a loss.

Update

I've tried a few more things, and still no luck. I ran the application out-of-browser with the same symptoms, promoted the SaveFileDialog to a class variable. The application behaves like there is no error, but no file appears in the save location (my Documents folder, in Vista).

The Plot Thickens

I was stepping through with the debugger and found additional strange behavior. After the call to saveDialog.OpenFile(), the file appears at the target location. It remains after each statement, but is removed after the call to writer.Close(). Why on earth would the file be automagically deleted when the stream closes?

Thanks in advance for your help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜