-1

I was just at Open directory dialog, and they said "get this package, and do this and this to get a folder select window to show up". Well, that all works great, using the Windows API Code Pack-Shell package. However, now I want to get the actual folder that is selected. I didn't notice them mentioning this anywhere.

I tried to do string folderLocation = Convert.ToString(dialog); (dialog is the variable for opening the folder window), but that only gave me like the property of the variable. I also tried this: CommonFileDialogResult result = dialog.ShowDialog(); string folderLocation = Convert.ToString(result);

But that just gave me "Ok" - which I take it is the result of it, instead of the actual folder.

Jumanji
  • 51
  • 1
  • 4

2 Answers2

0

The result of ShowDialog just indicates wether the user clicked OK, cancel, or just closed the window. CommonOpenFileDialog can be used for both files and folders, so it's a bit surprising when used as a folder picker, but the path is stored in FileName.

var dlg = new CommonOpenFileDialog();
dlg.IsFolderPicker = true;
if(dlg.ShowDialog() == CommonFileDialogResult.Ok) {
    Console.WriteLine(dlg.FileName);
}
gnud
  • 77,584
  • 5
  • 64
  • 78
0

If I understood correctly, you want to get Folder for a selected file? If that's the case, you can take FileInfo for that file, and extract folfer from it. Like this:

System.IO.FileInfo fInfo = new System.IO.FileInfo(oFD1.FileName);
MessageBox.Show(fInfo.DirectoryName);

PS. oFD1 is OpenFileDialog

Caldazar
  • 2,801
  • 13
  • 21