JefE
JefE

Reputation: 137

Setting root folder for FolderBrowser

How do i Set the root folder for a folderdialog?

My sample does not seem to work. (I checked that the folder exists)

    Dim FolderBrowserDialog1 As New FolderBrowserDialog

    FolderBrowserDialog1.RootFolder = "C:\VaultWorkspace\cadcampc\"

    If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
        Copy_Design_New_Loc.Text = FolderBrowserDialog1.SelectedPath
    End If

Error message

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string "C:\VaultWorkspace\cadcampc\" to type 'Integer' is not valid.

What do I need to do to set my custom location as rootfolder?

Upvotes: 6

Views: 26811

Answers (5)

Zayd
Zayd

Reputation: 31

I found a simple solution but I don't know if it exists on earlier VS as I have Visual studio 2022 but here it is anyway for future programmers:

Dim folderbrowser1 As New FolderBrowserDialog
folderbrowser1.InitialDirectory = path
folderbrowser1.ShowDialog()

Upvotes: 0

CodeCatia
CodeCatia

Reputation: 127

JefE, you asked if there is a way to use another root folder than the predefined special folders? have you tried with Shell.BrowseForFolder Method?

Try this:

    Const WINDOW_HANDLE = 0
    Const NO_OPTIONS = &H10&
    Const RootFolder = "C:\VaultWorkspace\"


    Dim objShell As Object = CreateObject("Shell.Application")
    Dim objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, "Select Folder:", NO_OPTIONS, RootFolder)

    If Not objFolder Is Nothing Then
        Copy_Design_New_Loc.Text = objFolder.self.path
    Else
        'Exit on Cancel
        Exit Sub
    End If

enter image description here

Upvotes: 0

DWE
DWE

Reputation: 137

Came across this question - not a regular here ... but thought I could post something useful, which people looking for a similar answer might appreciate ...

I use the following function to return the value of a selected folder ...

Imports System.Diagnostics.Process
Imports System.Windows.Forms

...

    Public Function SetWorkingPath() As String
        Try
            Dim folderDlg As New System.Windows.Forms.FolderBrowserDialog

            With folderDlg
                .ShowNewFolderButton = True
                .Description = "Selected your working folder. This is where your PDF files will be saved."
                .RootFolder = Environment.SpecialFolder.MyComputer
                .SelectedPath = IIf(Len(Trim(WorkingPath)) = 0, Environment.SpecialFolder.MyComputer, WorkingPath)
                If (.ShowDialog() = DialogResult.OK) Then
                    SetWorkingPath = .SelectedPath
                Else
                    SetWorkingPath = ""
                End If
            End With
        Catch e As Exception
            MsgBox(e.Message + " (" + e.ToString() + ")", MsgBoxStyle.Critical, "SetWorkingPath Error")
            SetWorkingPath = ""
        End Try

        WorkingPath = SetWorkingPath
    End Function

Hope this helps someone ...

DWE

Upvotes: 0

rheitzman
rheitzman

Reputation: 2297

FolderBrowserDialog has always been a margin tool IMO.

When opening a standard mapped folder you can use RootFolder to remove some clutter. SelectedPath will open the parent folder and highlight your folder but it may be off screen. Your posted path may look OK as it most likely has a small number of folders to display and the selected one should be visible.

    FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer
    FolderBrowserDialog1.SelectedPath = "C:\temp"
    If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        MsgBox(FolderBrowserDialog1.SelectedPath)
    End If

Tested on Win7 .Net 4 VS2013 VB.Net WinForms


Here's a variation that doesn't need the control on the form:

    Using fbd As New FolderBrowserDialog
        fbd.RootFolder = Environment.SpecialFolder.MyComputer
        fbd.SelectedPath = "H:\temp\scans"
        If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
            MsgBox(fbd.SelectedPath)
        End If
    End Using

Here's a way to use the OpenFileDialog, far from perfect but better than folder dialog IMO, and simpler than a subclass:

    Using obj As New OpenFileDialog
        obj.Filter = "foldersOnly|*.none"
        obj.CheckFileExists = False
        obj.CheckPathExists = False
        obj.InitialDirectory = "C:\temp"
        obj.CustomPlaces.Add("H:\OIS") ' add your custom location, appears upper left
        obj.CustomPlaces.Add("H:\Permits") ' add your custom location
        obj.Title = "Select folder - click Open to return opened folder name"
        obj.FileName = "OpenFldrPath"
        If obj.ShowDialog = Windows.Forms.DialogResult.OK Then
            MsgBox(IO.Directory.GetParent(obj.FileName).FullName)
        End If
    End Using

Upvotes: 8

Derek
Derek

Reputation: 8763

I would suggest using FolderBrowserDialogEx: A C# customization of FolderBrowserDialog.

http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=159352

It might be a pain to run it through an online code translator though ( to change it to VB .NET ). This folder browser is MUCH better than the regular one.

Upvotes: 0

Related Questions