Setting root folder for FolderBrowser
Asked Answered
G

5

6

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?

Granddaughter answered 3/9, 2015 at 8:22 Comment(4)
But with a so clear error message why not trying to understand it, rather than posting a question here? It says that an Integer is expected. Why not checking what RootFolder is (just putting the mouse on the variable would be enough to know that it is a System.Environment.SpecialFolder); after a quick search you would get: msdn.microsoft.com/en-us/library/… It is an Enum (= can be accessed via indices or by typing the whole values, where VS can be very helpful). That is: you cannot input anything, but have to choose among a list of folders.Sabelle
But, there must be a way to use another folder than the predefined special folders?Granddaughter
Your question was "What do I do wrong?" and my answer was that you should have known it after a quick analysis. The question "is there any other alternative?" is different. In any case, I can also answer this one: by default (= using FolderBrowserDialog as it is) there is none. On the other hand, it does not seem to be a big deal (there quite a few special folders and the initial folder is not a so important feature). If you think otherwise, you should create your own approach.Sabelle
Use SelectedPath instead of RootFolder when starting in a standard folder. This issue has been a weak point forever with this dialog. The next issue you'll see even when you do get it to start in a folder the selected item is often off screen and the user has to scroll down and know what to look for. Search use openfiledialog for folder selection for alternatives.Dendroid
D
8

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
Dendroid answered 3/9, 2015 at 15:14 Comment(0)
A
0

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.

Age answered 3/9, 2015 at 17:28 Comment(0)
C
0

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

Caustic answered 24/12, 2017 at 9:48 Comment(0)
A
0

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

Arguello answered 15/1, 2019 at 16:14 Comment(0)
M
0

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()
Martynne answered 2/2, 2023 at 8:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.