use the openfiledialog to folder level

Using the code i have below (allows you to select files), how can I just select a folder? Thanks!

Dim FileDlg as new OpenFileDialog()

openfildlg.filter = "All files (*.*)|*.*"

openfiledlg.restoredirectory = true

if openfiledlg.showdialog() = dialogresult.OK then

lblfile.text = Openfiledlg.filename

end if

[358 byte] By [project2n5e0o1] at [2007-12-24]
# 1

Hi,

you could use a FolderBrowserDialog instead of OpenFileDialog for selecting a folder:

Using dialog As New FolderBrowserDialog()
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim folder As String = dialog.SelectedPath
' Process folder here...
End If
End Using

Would something like that work for you?

Andrej

AndrejTozon at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Thaks for your response. I can't seem to get the code you provided to work for me. It does not like the syntax of the first line. I am using VS 2002. any other suggestions?

thanks!

project2n5e0o1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

Yes, the previously posted code only works with VS2005 / .NET 2.0. Please see if the following modifications works for you:

Dim dialog As New FolderBrowserDialog()
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK
Then
Dim folder As String
= dialog.SelectedPath
' Process folder here...
End
If
dialog.Dispose()

Then again... it might not work after all. Looks like FolderBrowserDialog was introduced in VS2003 / .NET 1.1...

Andrej

AndrejTozon at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

No there is no method called FolderBrowserDialog().

I have been doing some searching and a guys said this worked for him:

Dim oFolder As Shell32.Folder
Dim oShell As Shell32.Shell = New Shell32.Shell()

oFolder = oShell.BrowseForFolder(0, "Select a Folder:", 0, "C:\")

how would I import a way to use the Shell32 objects?
project2n5e0o1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

Just found this sample on the net...

Andrej

AndrejTozon at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

That looks like a good solution. I looked at it and it seemed too confusing for me though. I found one solution that did work for me. You have to add the System.Design reference and use the class below.

Call the class by saying: lblfilename.text = Browsefolderdialog.showdialog("Select directory")

I appreciate your help!

Class is listed below:

'if any suggestion please send mail to thulasi.sekaran@gmail.com

Public Class ClassBrowseFolder
Inherits System.Windows.Forms.Design.FolderNameEditor ' Reference System.Design.Dll

Dim MyBrowseFolder As New FolderBrowser()

Public Function ShowDialog(ByVal browseFolderTitle As String)
With MyBrowseFolder
.Style = FolderBrowserStyles.BrowseForComputer
'.Style = FolderBrowserStyles.BrowseForEverything
'.Style = FolderBrowserStyles.BrowseForPrinter
'.Style = FolderBrowserStyles.RestrictToDomain
'.Style = FolderBrowserStyles.RestrictToFilesystem
'.Style = FolderBrowserStyles.RestrictToSubfolders
'.Style = FolderBrowserStyles.ShowTextBox
.StartLocation = FolderBrowserFolder.MyDocuments
'.StartLocation = FolderBrowserFolder.Desktop
'.StartLocation = FolderBrowserFolder.Favorites
'.StartLocation = FolderBrowserFolder.MyComputer
'.StartLocation = FolderBrowserFolder.MyPictures
'.StartLocation = FolderBrowserFolder.NetAndDialUpConnections
'.StartLocation = FolderBrowserFolder.NetworkNeighborhood
'.StartLocation = FolderBrowserFolder.Printers
'.StartLocation = FolderBrowserFolder.Recent
'.StartLocation = FolderBrowserFolder.SendTo
'.StartLocation = FolderBrowserFolder.StartMenu
'.StartLocation = FolderBrowserFolder.Templates
.Description = browseFolderTitle ' For Dialog box title
.ShowDialog()
ShowDialog = .DirectoryPath ' Return the Folder path
End With
End Function

End Class

project2n5e0o1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...