【问题标题】:SaveFileDialog does not save the filename or pathSaveFileDialog 不保存文件名或路径
【发布时间】:2016-01-28 20:44:23
【问题描述】:

我想使用 SaveFileDialog,当单击 Save 按钮时,我想将文件名和路径保存到单独的变量中。代码如下:

 private void Button_Click(object sender, EventArgs e)
    {
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.FileName = "SaveFile";
        saveFileDialog1.DefaultExt = ".txt";
        saveFileDialog1.Filter = "Text Files (*.txt)|*.txt";
        saveFileDialog1.Title = "Save a Text File";
        saveFileDialog1.FileOk += saveFileDialog1_FileOk;
        saveFileDialog1.ShowDialog();
    }

    private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
    {
            string filename = System.IO.Path.GetFileName(saveFileDialog1.FileName);
            string name = saveFileDialog1.FileName;

            var test = System.IO.Path.GetDirectoryName(saveFileDialog1.FileName);
        }

对话框打开并触发 saveFileDialog1_FileOk 事件,但我得到一个空字符串作为文件名,并且获取路径(没有文件名)的解决方案不起作用。我做错了什么?

【问题讨论】:

  • 看看这个,你会很快发现你的问题/问题出在哪里。msdn.microsoft.com/en-us/library/…
  • 试过了,但它甚至没有跳转到 if 分支
  • 我为什么投了反对票?

标签: c# winforms path filenames savefiledialog


【解决方案1】:

您遇到的主要问题是使用SaveFileDialog 的2 个实例。

您显示一个对话框,然后尝试从另一个显然为空的对话框中读取File

请注意,在您的按钮单击中,您正在创建一个新的本地实例并显示它,然后在FileOk 中,您正在使用另一个似乎是表单级别成员的实例。


修复 1: 您可以简单地删除 SaveFileDialog saveFileDialog1 = new SaveFileDialog();,因为您似乎将 saveFileDialog1 作为表单的成员。

修复 2: 你可以这样使用SaveFileDialog

var sfd= new SaveFileDialog();
//Other initializations ...
//sfd.Filter= "Text files (*.txt)|*.txt|All files (*.*)|*.*";
//sfd.DefaultExt = "txt";

if(sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    MessageBox.Show(sfd.FileName);
    //ِDo something for save
}
else
{
    //Do something for cancel if you want
}

然后您可以使用 FileName 属性访问选定的文件,例如 MessageBox.Show(sfd.FileName);

【讨论】:

  • @MethodMan 注意,OP 在Button_Click 中创建了一个新的本地实例并显示它,然后在FileOk 他使用saveFileDialog1.FileName,这与他显示的实例明显不同。跨度>
  • @MethodMan 你肯定不是盲人,我戴眼镜,我的眼镜指出我的重点;)
  • 那么如何避免呢?一切正常 - 只是保存不起作用
  • 我现在为您发布修复程序
  • @Jackson30 你的代码有问题吗?您正在显示一个对话框并从其他明显为空的对话框中读取 FileName :)
【解决方案2】:

从 MSDN (https://msdn.microsoft.com/de-de/library/system.windows.forms.savefiledialog(v=vs.110).aspx) 查看这个示例:

private void button1_Click(object sender, System.EventArgs e)
 {
     Stream myStream ;
     SaveFileDialog saveFileDialog1 = new SaveFileDialog();

     saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
     saveFileDialog1.FilterIndex = 2 ;
     saveFileDialog1.RestoreDirectory = true ;

     if(saveFileDialog1.ShowDialog() == DialogResult.OK)
     {
         if((myStream = saveFileDialog1.OpenFile()) != null)
         {
             // Code to write the stream goes here.
             myStream.Close();
         }
     }
 }

只需要查看saveFileDialog1.ShowDialog()的返回值就可以知道用户是否点击了ok。

然后,您可以使用包含所选文件路径的FileName 属性。

编辑:要获取文件的文件夹路径,您可以使用:

string folderPath = new DirectoryInfo(saveFileDialog1.FileName).Name;

【讨论】:

  • 文件夹路径中的参数异常。格式无效
  • saveFileDialog1.FileName 属性有什么值?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 2022-07-08
  • 2014-06-14
  • 1970-01-01
相关资源
最近更新 更多