Obtain file path of C# save dialog box

2019-01-14 07:37发布

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?

标签: c# dialog
4条回答
Root(大扎)
2楼-- · 2019-01-14 08:00

Addressing the textbox...

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
    this.textBox1.Text = saveFileDialog.FileName;
}
查看更多
在下西门庆
3楼-- · 2019-01-14 08:01

Try below code.

saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
查看更多
神经病院院长
4楼-- · 2019-01-14 08:02
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();
        }
    }
}
查看更多
趁早两清
5楼-- · 2019-01-14 08:03

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.

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
} 
查看更多
登录 后发表回答