Question

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?

Was it helpful?

Solution

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
} 

OTHER TIPS

Addressing the textbox...

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
    this.textBox1.Text = saveFileDialog.FileName;
}
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();
        }
    }
}

Try below code.

saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top