Link to home
Start Free TrialLog in
Avatar of curmudgeon42
curmudgeon42

asked on

Browse for Directory in VB.NET

I need to have the user pick a directory on the hard disk, not a file.  Can you do this with an OpenFileDialog?  Do you need another control?  What control?  Can you include a snippet of code to show me how to create this directory picker?
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Joe_Griffith
Joe_Griffith

Imports System.Windows.Forms
Imports System.Windows.Forms.Design

Public Class clsSpFolderBrowser
  Inherits FolderNameEditor
  Dim mo_FB As New FolderBrowser()

  Public Sub New()
    MyBase.new()
    mo_FB.Style = FolderNameEditor.FolderBrowserStyles.RestrictToFilesystem
    mo_FB.StartLocation = FolderBrowserFolder.Desktop ' Seems to be limited to this enumeration
  End Sub

  Public Sub ShowDialog()
    mo_FB.ShowDialog()
  End Sub

  Public ReadOnly Property DirectoryPath() As String
    Get
      Return mo_FB.DirectoryPath
    End Get
  End Property

  Public WriteOnly Property Description() As String
    Set(ByVal Value As String)
      mo_FB.Description = Value
    End Set
  End Property
End Class
   
Dim po_FolderBrowser As New clsSpFolderBrowser()
    po_FolderBrowser.Description = "Select a folder"
    po_FolderBrowser.ShowDialog()
    If po_FolderBrowser.DirectoryPath <> "" Then
      x = po_FolderBrowser.DirectoryPath
    End If
Avatar of curmudgeon42

ASKER

Thanks that was perfect.