I’d like to be able to select directories (in addition to single files) in the dialog. As it is now, when I click ‘open’ when the directory is selected, it descends into the directory, while I want it to return the path to the directory.
Additionally, how would I read such path? I don’t see any property that would return the full path.
The easy way is to use a
FolderBrowserDialoginstead.This dialog is designed to allow the user to select a folder. You can also enable the user to create a new folder, if you like, straight from the dialog box.
You shouldn’t try and allow the user to select either a file or a folder from the same dialog. I don’t think I’ve ever seen that done in an application. Consider how difficult it would be to distinguish between users expanding directories to locate files, and users who are attempting to select the directory itself. This is why two separate dialogs have been provided; choose the one that best suits your purposes.
As far as reading the full path to the file selected in an
OpenFileDialog, the property you want is (somewhat confusingly) calledFileName. The documentation explains that:Or, if you’re allowing multiple selection, you’ll likely want to use the
FileNamesproperty instead.