您可以在此处下载 Hans 建议的工作示例。
为方便起见,这里是使用带有按钮和字段的简单表单的精简版本,但也包括其他标志及其描述。
Imports System.Runtime.InteropServices
Public Class FoldersOnly
<Flags()> _
Public Enum BrowseInfoFlags As UInteger
''' <summary>
''' Only return file system directories.
''' </summary>
BIF_RETURNONLYFSDIRS = &H1
''' <summary>
''' Do not include network folders below the domain level in the dialog box's tree view control.
''' </summary>
BIF_DONTGOBELOWDOMAIN = &H2
''' <summary>
''' Include a status area in the dialog box.
''' The callback function can set the status text by sending messages to the dialog box.
''' This flag is not supported when <bold>BIF_NEWDIALOGSTYLE</bold> is specified
''' </summary>
BIF_STATUSTEXT = &H4
''' <summary>
''' Only return file system ancestors.
''' An ancestor is a subfolder that is beneath the root folder in the namespace hierarchy.
''' If the user selects an ancestor of the root folder that is not part of the file system, the OK button is grayed
''' </summary>
BIF_RETURNFSANCESTORS = &H8
''' <summary>
''' Include an edit control in the browse dialog box that allows the user to type the name of an item.
''' </summary>
BIF_EDITBOX = &H10
''' <summary>
''' If the user types an invalid name into the edit box, the browse dialog box calls the application's BrowseCallbackProc with the BFFM_VALIDATEFAILED message.
''' This flag is ignored if <bold>BIF_EDITBOX</bold> is not specified.
''' </summary>
BIF_VALIDATE = &H20
''' <summary>
''' Use the new user interface.
''' Setting this flag provides the user with a larger dialog box that can be resized.
''' The dialog box has several new capabilities, including: drag-and-drop capability within the
''' dialog box, reordering, shortcut menus, new folders, delete, and other shortcut menu commands.
''' </summary>
BIF_NEWDIALOGSTYLE = &H40
''' <summary>
''' The browse dialog box can display URLs. The <bold>BIF_USENEWUI</bold> and <bold>BIF_BROWSEINCLUDEFILES</bold> flags must also be set.
''' If any of these three flags are not set, the browser dialog box rejects URLs.
''' </summary>
BIF_BROWSEINCLUDEURLS = &H80
''' <summary>
''' Use the new user interface, including an edit box. This flag is equivalent to <bold>BIF_EDITBOX | BIF_NEWDIALOGSTYLE</bold>
''' </summary>
BIF_USENEWUI = (BIF_EDITBOX Or BIF_NEWDIALOGSTYLE)
''' <summary>
''' hen combined with <bold>BIF_NEWDIALOGSTYLE</bold>, adds a usage hint to the dialog box, in place of the edit box. <bold>BIF_EDITBOX</bold> overrides this flag.
''' </summary>
BIF_UAHINT = &H100
''' <summary>
''' Do not include the New Folder button in the browse dialog box.
''' </summary>
BIF_NONEWFOLDERBUTTON = &H200
''' <summary>
''' When the selected item is a shortcut, return the PIDL of the shortcut itself rather than its target.
''' </summary>
BIF_NOTRANSLATETARGETS = &H400
''' <summary>
''' Only return computers. If the user selects anything other than a computer, the OK button is grayed.
''' </summary>
BIF_BROWSEFORCOMPUTER = &H1000
''' <summary>
''' Only allow the selection of printers. If the user selects anything other than a printer, the OK button is grayed
''' </summary>
BIF_BROWSEFORPRINTER = &H2000
''' <summary>
''' The browse dialog box displays files as well as folders.
''' </summary>
BIF_BROWSEINCLUDEFILES = &H4000
''' <summary>
''' The browse dialog box can display shareable resources on remote systems.
''' </summary>
BIF_SHAREABLE = &H8000
''' <summary>
''' Allow folder junctions such as a library or a compressed file with a .zip file name extension to be browsed.
''' </summary>
BIF_BROWSEFILEJUNCTIONS = &H10000
End Enum
Private Structure BrowseInfo
Public hWndOwner As IntPtr
Public pidlRoot As Integer
Public sDisplayName As String
Public sTitle As String
Public ulFlags As Integer
Public lpfn As Integer
Public lParam As Integer
Public iImage As Integer
End Structure
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal nPidl As Integer, ByVal pszPath As String) As Integer
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (ByRef lpBrowseInfo As BrowseInfo) As Integer
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal PV_Renamed As Integer)
Private Const BIF_RETURNONLYFSDIRS As Short = &H1S
Private Const BIF_DONTGOBELOWDOMAIN As Short = &H2S
Private Const BIF_STATUSTEXT As Short = &H4S
Private Const BIF_RETURNFSANCESTORS As Short = &H8S
Private Const BIF_BROWSEFORCOMPUTER As Short = &H1000S
Private Const BIF_BROWSEFORPRINTER As Short = &H2000S
Private Const MAX_PATH As Short = 256
' Let the user browse for a folder.
' Return the folder or Nothing if the user cancels.
Public Function BrowseForFolder(ByRef dialog_title As _
String, ByRef frm As Form) As String
Try
Dim browse_info As BrowseInfo
With browse_info
.hWndOwner = frm.Handle
.pidlRoot = 0
.sDisplayName = Space$(260)
.sTitle = dialog_title
.ulFlags = BrowseInfoFlags.BIF_RETURNONLYFSDIRS
.lpfn = 0
.lParam = 0
.iImage = 0
End With
Dim item As Integer = _
SHBrowseForFolder(browse_info)
Dim path As String = New String(" "c, MAX_PATH)
If SHGetPathFromIDList(item, path) = 0 Then
path = Nothing
Else
path = path.Trim
End If
CoTaskMemFree(item)
Return path
Catch ex As Exception
MessageBox.Show(ex.Message)
Return Nothing
End Try
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
txtNetworkDrive.Text = BrowseForFolder("test", Me)
End Sub
End Class