• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1548
  • Last Modified:

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?
0
curmudgeon42
Asked:
curmudgeon42
  • 2
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
Joe_GriffithCommented:
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
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
curmudgeon42Author Commented:
Thanks that was perfect.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now