Mister Dev
Mister Dev

Reputation: 10451

Obtain file path of C# save dialog box

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?

Upvotes: 17

Views: 65501

Answers (4)

user4218087
user4218087

Reputation: 1

Try below code.

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

Upvotes: -1

Patrick Desjardins
Patrick Desjardins

Reputation: 140993

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
} 

Upvotes: 47

Nidz
Nidz

Reputation: 131

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();
        }
    }
}

Upvotes: 3

Inisheer
Inisheer

Reputation: 20794

Addressing the textbox...

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

Upvotes: 4

Related Questions