How do you get path and filename from a SaveFileDialog ?

C

Csharper95

I want to get the file name and path from a SaveFileDialog (for a
'save as' operation) after the user has saved the information under a
new file name (and presumably under a new path) ?

I want to display the new file name and path in the Form.text Title.
So how do I go about getting the info ?

Heres my code:


private void menu_profile_saveas_Click(object sender,
System.EventArgs e)
{
//Open file for reading
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = geoData.GetFilePath();
saveFileDialog1.Filter = "DAT31-8 Files
(*.N31)|*.n31|All Files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true ;
//read and filter the raw data
if(saveFileDialog1.ShowDialog() ==
DialogResult.OK)
{
geoData.SaveToN31File(saveFileDialog1.FileName);

}

}

Could someone please help ?
 
K

Kai Brinkmann [MSFT]

I'm a little confused. Your code is already retrieving the path and file
selected by the user (saveFileDialog1.FileName). So all you have to do is
the following:

this.Text = saveFileDialog1.FileName;

Of course this string includes the path and extension as well. If you want
to extract path, file name, and extension separately you can use the
FileInfo class:

FileInfo fi = new FileInfo(saveFileDialog1.FileName);

Then you can use the properties of the FileInfo object to retrieve the
information you want:

fi.DirectoryName \\ the directory's full path
fi.Name \\ the file name
fi.Extension \\ the file extension

Hope this helps.
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

Csharper95 said:
I want to get the file name and path from a SaveFileDialog (for a
'save as' operation) after the user has saved the information under a
new file name (and presumably under a new path) ?

I want to display the new file name and path in the Form.text Title.
So how do I go about getting the info ?

Heres my code:


private void menu_profile_saveas_Click(object sender,
System.EventArgs e)
{
//Open file for reading
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = geoData.GetFilePath();
saveFileDialog1.Filter = "DAT31-8 Files
(*.N31)|*.n31|All Files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true ;
//read and filter the raw data
if(saveFileDialog1.ShowDialog() ==
DialogResult.OK)
{
geoData.SaveToN31File(saveFileDialog1.FileName);

}

}

Could someone please help ?
 
W

Wayne

Or you can use:

Path.GetDirectoryName(saveFileDialog1.FileName);
Path.GetFileName(saveFileDialog1.FileName);
Path.GetExtension(saveFileDialog1.FileName);

No need to create a FileInfo object this way, not like it should be that
expensive.

--
Thanks
Wayne Sepega
Jacksonville, Fl


"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein

Kai Brinkmann said:
I'm a little confused. Your code is already retrieving the path and file
selected by the user (saveFileDialog1.FileName). So all you have to do is
the following:

this.Text = saveFileDialog1.FileName;

Of course this string includes the path and extension as well. If you want
to extract path, file name, and extension separately you can use the
FileInfo class:

FileInfo fi = new FileInfo(saveFileDialog1.FileName);

Then you can use the properties of the FileInfo object to retrieve the
information you want:

fi.DirectoryName \\ the directory's full path
fi.Name \\ the file name
fi.Extension \\ the file extension

Hope this helps.
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top