Is there a way to using a dialog window to get the folder path without name file?
Asked
Active
Viewed 1e+01k times
2 Answers
61
Though an old question,
I didn't like that uglyFolderBrowserDialog
, so here's a trick that worked for me, it uses the SaveFileDialog
// Prepare a dummy string, thos would appear in the dialog
string dummyFileName = "Save Here";
SaveFileDialog sf = new SaveFileDialog();
// Feed the dummy name to the save dialog
sf.FileName = dummyFileName;
if(sf.ShowDialog() == DialogResult.OK)
{
// Now here's our save folder
string savePath = Path.GetDirectoryName(sf.FileName);
// Do whatever
}

user3699569
- 113
- 6

Sqrs
- 805
- 6
- 6
-
8Adding dialog.CheckFileExists = false; will mean the use can browse around folders without selecting a file, the dummy will be accepted. – Marc Jun 14 '14 at 11:27
-
7Which is false by default for `SaveFileDialog`. – C.Evenhuis Apr 16 '15 at 19:01
-
The draw back is that If the file name is blank the user can't click ok. :( – Pedro77 Mar 01 '17 at 16:05
-
1Thank you for this. I have been looking for this solution for quite a long time. – fdsafdsafdsafdsafs May 25 '18 at 15:08
-
3I've also added `sf.Filter = "Directory | directory";` to hide all the files to the user. – laurentIsRunning Dec 11 '19 at 14:45
44
Check the FolderBrowserDialog
// Bring up a dialog to chose a folder path in which to open or save a file.
private void folderMenuItem_Click(object sender, System.EventArgs e)
{
var folderBrowserDialog1 = new FolderBrowserDialog();
// Show the FolderBrowserDialog.
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
{
string folderName = folderBrowserDialog1.SelectedPath;
... //Do your work here!
}
}
-
-
1too long! in that example tells how to open a rtf file not find out how to select a folder – Luca's Sep 07 '11 at 08:03