Thursday, July 14, 2011

Selecting a Folder in VB.NET

To go along with my previous post on the Open File and Save File dialogs in VB.NET, I am providing this post using FolderBrowserDialog to select a specific Directory.

Browsing to Select a Directory in VB.NET
     Dim dirDlg As New FolderBrowserDialog

dirDlg.RootFolder = Environment.SpecialFolder.MyComputer

If dirDlg.ShowDialog() = DialogResult.OK Then

' do something with the selected path
Debug.Print("Directory: " + dirDlg.SelectedPath)

Else

' the dialog was cancelled

End If


The FolderBrowsingDialog object has many options, but I have only shown the most commonly used one in the example. The RootFolder property lets you specify the directory to start browsing from. You can set this property to an explicit path (like "c:\MyData") or you can use the special directories that are defined for you:
Environment.SpecialFolder.MyDocument
Environment.SpecialFolder.MyComputer
Environment.SpecialFolder.MyPictures
Environment.SpecialFolder.Desktop

The default is Environment.SpecialFolder.MyComputer.

After the ShowDialog call returns OK, use the SelectedPath property to get the selected directory.

1 comment:

Cegonsoft said...

Can you say? Which E-Book is best for to learn the .Net , Mainly for beginners?
Cegonsoft