Selecting multiple directories with the FolderBrowserDialog in .NET 9
One of the smaller updates to make it into .NET 9 for WinForms was allowing multi-selection in the FolderBrowserDialog. Let's see how.
It's great that, even after so many years, the teams at Microsoft continue to add updates to their oldest technologies with every .NET release. WinForms recently got a particularly small one, in .NET 9, that allows the FolderBrowserDialog to select multiple directories instead of one, so let's check it out (it won't take long, lol).
The old FolderBrowserDialog (one at a time, please)
The FolderBrowserDialog control has always given us an easy way to select a single folder in an app. After confirming the user pressed OK, we just read in the SelectedPath
property and move on with life:
if (fbd.ShowDialog() == DialogResult.OK)
MessageBox.Show($"The path to process:\n\n{fbd.SelectedPath}");
There's a lot of other available properties with this control too, but they didn't change so I won't bother with them. They're there though. 😏
The new FolderBrowserDialog (the more, the merrier)
The only change we have to make with the updated control in .NET 9 is to set the Multiselect
property to true
, either in the designer or at runtime:
fbd.Multiselect = true;
Then we call it the same way but reference the new SelectedPaths
property, which is an array of strings:
if (fbd.ShowDialog() == DialogResult.OK)
MessageBox.Show($"The path(s) to process:\n\n{string.Join("\n", fbd.SelectedPaths)}");
That's it! I can't decide if it's stranger that it never had this capability, or that someone decided to add it now after all these years. Did something else change that made this a priority? Or is someone who's been with WinForms since the very beginning retiring, and this was on their bucket list? Either way, we could only choose one folder at a time before, and now we can choose as many as we'd like!
Spread the Word