获取C#保存对话框的文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/180330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:51:18  来源:igfitidea点击:

Obtain file path of C# save dialog box

c#dialog

提问by Mister Dev

I've got a save dialog box which pops up when i press a button. However i dont want to save a file at that point, i want to take the name and place it in the text box next to the button, for the name to be used later.

我有一个保存对话框,当我按下按钮时会弹出该对话框。但是我当时不想保存文件,我想取名称并将其放在按钮旁边的文本框中,以便稍后使用该名称。

Can anybody tell me how to obtain the file path from the save dialog box to use it later?

谁能告诉我如何从保存对话框中获取文件路径以供以后使用?

采纳答案by Patrick Desjardins

Here is a sample code I just wrote very fast... instead of Console.Write you can simply store the path in a variable and use it later.

这是我刚刚写的非常快的示例代码......而不是 Console.Write 你可以简单地将路径存储在一个变量中并在以后使用它。

SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments); 
saveFileDialog1.Filter = "Your extension here (*.EXT)|*.ext|All Files (*.*)|*.*" ; 
saveFileDialog1.FilterIndex = 1; 

if(saveFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    Console.WriteLine(saveFileDialog1.FileName);//Do what you want here
} 

回答by Inisheer

Addressing the textbox...

解决文本框...

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
    this.textBox1.Text = saveFileDialog.FileName;
}

回答by Nidz

private void mnuFileSave_Click(object sender, EventArgs e)
{
    dlgFileSave.Filter = "RTF Files|*.rtf|"+"Text files (*.txt)|*.txt|All files (*.*)|*.*";
    dlgFileSave.FilterIndex = 1;
    if (dlgFileSave.ShowDialog() == System.Windows.Forms.DialogResult.OK && dlgFileSave.FileName.Length > 0)
    {
        foreach (string strFile in dlgFileSave.FileNames)
        {
            SingleDocument document = new SingleDocument();
            document.rtbNotice.SaveFile(strFile, RichTextBoxStreamType.RichText);
            document.MdiParent = this;
            document.Show();
        }
    }
}

回答by user4218087

Try below code.

试试下面的代码。

saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);