Boardy
Boardy

Reputation: 36237

Select the default path of the FolderBrowserDialog in c# wpf

I am currently working on a C# WPF project. I have a FolderBrowserDialog in the System.Windows.Forms namespace. I am creating an instance of the dialog with a variable named dlg and assigning the selected path to My Documents by using the following line of code:

dlg.SelectedPath = Environment.SpecialFolder.MyDocuments.ToString();

However, this doesn't seem to make much difference. I then tried doing the same thing but with the root path but this just seems to make it that it will set the root as My Documents and you can't get out of My Documents, i.e. to C:\ or Desktop.

How can I set a default path but still allowing access to all available areas of the drive, e.g. default path to be My Documents but allow the user to go outside of My Documents to C:\ or desktop.

Thanks for any help you can provide.

Upvotes: 3

Views: 6408

Answers (1)

adrianbanks
adrianbanks

Reputation: 83004

You are assigning the wrong value to SelectedPath. By setting Environment.SpecialFolder.MyDocuments.ToString(), you are setting the string "MyDocuments" (or "Personal" as it has the same value in the Environment.SpecialFolder enum) to the SelectedPath. This cannot be found as it is not a valid path, so nothing gets selected.

You need to lookup the path of the special folder using Environment.GetFolderPath():

dlg.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

This will set the path of the special folder, which the folder browse dialog will select when it opens.

Upvotes: 4

Related Questions