【问题标题】:SaveAs Dialog not saving file from network locationSaveAs 对话框不从网络位置保存文件
【发布时间】:2021-01-27 19:28:26
【问题描述】:

我有一个保存按钮,它向服务器发出查询,该服务器返回共享驱动器上电子邮件的文件路径,例如“F:\store\email1.eml”

private void SaveAsBtn_Click(object sender, RoutedEventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                connection.Open();
                using (var cmd = new SqlCommand("spGetDoc", connection) { CommandType = System.Data.CommandType.StoredProcedure })
                {
                    SqlParameter param = new SqlParameter("@GUID", ConfirmedGuidBox.Text);
                    cmd.Parameters.Add(param);

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            string fileSource = rdr.GetString(1);

                            Stream myStream;
                            SaveFileDialog saveFileDialog = new SaveFileDialog();
                            saveFileDialog.Filter = "Emails|*.eml";
                            saveFileDialog.FileName = fileSource;
                            saveFileDialog.ShowDialog();
                        }
                    }
                }
            }
        }

这会按预期打开另存为对话框,但我无法正确加载文件。预设文件名计算为\\server\store\email1.emlc#,如果单击“保存”,它将尝试将文件保存在相同的共享位置,而不是导航到对话框内的位置。将完整路径缩短为仅 email1.eml 意味着不会保存任何内容。

看来saveFileDialog.FileName 并没有真正打开fileSource 中的文件,只是设置了默认名称。我怎样才能让它工作,以便我能够保存数据库查询中指定的文件的副本?

【问题讨论】:

  • 对话框不会打开文件,它只是让用户选择一个文件名。 ShowDialog返回true后,您必须自己打开文件saveFileDialog.FileName
  • @KlausGütter 我试图模仿的机制类似于您如何通过右键单击“另存为”在 Firefox 中保存网页的 html 文件副本,并自动填充文件.如果用户无论如何都必须去查找文件,我的程序中保存功能的要点就有点过时了。所以这在 WPF 中是不可能的?
  • 不要在阅读器循环中执行复杂或阻塞的操作将数据返回到客户端应用程序中,然后执行您需要的任何操作

标签: c# wpf


【解决方案1】:

听起来您正在尝试设置文件保存对话框打开的初始目录。看 Setting the initial directory of an SaveFileDialog?

【讨论】:

  • 我不太确定。设置文件名和初始目录似乎仍然没有将实际文件本身加载到对话框中。
  • 也许你可以更好地解释你想要做什么,这是一个简单的powershell测试,你可以执行Add-Type -AssemblyName PresentationFramework$sfd = New-Object 'Microsoft.Win32.SaveFileDialog'#this will show no file in the filename of the dialog#this will show no file in the filename of the dialog$sfd.ShowDialog()$sfd.InitialDirectory = "C:\Temp"$sfd.FileName = "testFile.test"@ 987654328@$sfd.ShowDialog()
  • 一个感兴趣的问题@ancsjs:如果你已经有了文件的完整路径,为什么还要提示用户保存到哪里?
猜你喜欢
  • 1970-01-01
  • 2012-04-01
  • 2016-12-28
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
相关资源
最近更新 更多