Silverlight SaveFileStream 无提示地失败

发布于 2024-10-20 13:32:46 字数 1113 浏览 3 评论 0原文

我有一小段代码,似乎是从 Silverlight 4.0 保存文件的教科书示例,但它似乎不起作用。

以下代码片段来自按钮单击处理程序:

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!");
        writer.Flush();
        writer.Close();
    }
}

我尝试将文件保存到许多不同的位置,所有位置都具有相同的行为:

  • SaveFileDialog 似乎表现正常。
  • SaveFileStream(从调试器)显示表现正常。
  • 调用 writer.Flush() 后,BaseStream 前进到位置 15。
  • 不会引发异常。
  • 该块执行后,我无法使用 Windows 资源管理器找到该文件。

在我看来,代码太简单了,正常情况下不会失败。这引出了我的问题:我的情况出了什么问题?为什么显示保存成功,但文件却找不到?安全设置?代码本身?我不知所措。

更新

我又尝试了一些方法,但仍然没有运气。我在浏览器外运行该应用程序时出现了相同的症状,并将 SaveFileDialog 提升为类变量。应用程序的行为就像没有错误一样,但保存位置(Vista 中的“我的文档”文件夹)中没有出现任何文件。

情节变得更加丰富

我正在使用调试器进行单步调试,并发现了其他奇怪的行为。调用 saveDialog.OpenFile() 后,文件将出现在目标位置。它保留在每个语句之后,但在调用 writer.Close() 后被删除。到底为什么当流关闭时文件会被自动删除?

预先感谢您的帮助!

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!");
        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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文