以下示例代碼展示如何調用系統的文件夾選擇對話框:
1 Private Function SelectFolder(ByVal Describe As String, Optional ByVal ShowNewFolder As Boolean = True) As String 2 Using nOpen As New System.Windows.Forms.FolderBrowserDialog() 3 nOpen.Description = Describe 4 nOpen.ShowNewFolderButton = ShowNewFolder 5 nOpen.ShowDialog() 6 Return nOpen.SelectedPath 7 End Using 8 End Function
調用方式:
1 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 2 Dim _selectedFolder As String = SelectFolder("請選擇安裝目錄", True) '顯示選擇安裝目錄界面,同時允許創建新文件夾 3 MsgBox(_selectedFolder) 4 End Sub 5 6 Private Function SelectFolder(ByVal Describe As String, Optional ByVal ShowNewFolder As Boolean = True) As String 7 Using nOpen As New System.Windows.Forms.FolderBrowserDialog() 8 nOpen.Description = Describe 9 nOpen.ShowNewFolderButton = ShowNewFolder 10 nOpen.ShowDialog() 11 Return nOpen.SelectedPath 12 End Using 13 End Function
效果圖: