1

I'm hesitant to ask this question because I'm sure it must already be answered somewhere, but I've been searching for hours and either I'm drowning in all the noise or I simply can't find an elegant solution for VB.NET.

Essentially, my problem is identical to this question, but applied to VB.NET:

How do you configure an OpenFileDialog to select folders?

http://www.lyquidity.com/devblog/?p=136

Can someone please help direct me to the cleanest/most elegant solution?

I really appreciate it.

Zingapuro
  • 51
  • 8

1 Answers1

2

Agreed, the FolderBrowserDialog is awful. I use the standard SaveFileDialog, and ignore the filename.

Dim strFolder As String = ""
Using dlg As New SaveFileDialog With {.AddExtension = True,
                                      .AutoUpgradeEnabled = True,
                                      .CreatePrompt = False,
                                      .OverwritePrompt = False,
                                      .CheckFileExists = False,
                                      .CheckPathExists = True,
                                      .FileName = "Folder selection",
                                      .Filter = "All files (*.*)|*.*",
                                      .FilterIndex = 1,
                                      .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments,
                                      .SupportMultiDottedExtensions = True,
                                      .Title = "Select folder",
                                      .ValidateNames = True}
  If dlg.ShowDialog = DialogResult.OK Then
    Dim strFilename As String = dlg.FileName
    strFolder = System.IO.Path.GetDirectoryName(strFilename)
  End If
End Using
MsgBox(strFolder)
SSS
  • 4,807
  • 1
  • 23
  • 44