设置FolderBrowser的根文件夹

Jef*_*efE 3 vb.net folderbrowserdialog

如何设置folderdialog的根文件夹?

我的样本似乎不起作用.(我检查过该文件夹是否存在)

    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
Run Code Online (Sandbox Code Playgroud)

错误信息

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.
Run Code Online (Sandbox Code Playgroud)

如何将自定义位置设置为rootfolder,我需要做什么?

rhe*_*man 6

FolderBrowserDialog一直是IMO的保证金工具.

打开标准映射文件夹时,您可以使用它RootFolder来删除一些混乱.SelectedPath将打开父文件夹并突出显示您的文件夹,但它可能在屏幕外.您发布的路径可能看起来不错,因为它很可能只显示少量文件夹,并且所选文件夹应该可见.

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

在Win7 .Net 4 VS2013 VB.Net WinForms上测试


这是一个不需要对表单进行控制的变体:

    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
Run Code Online (Sandbox Code Playgroud)

这是一种使用OpenFileDialog远远不完美但比文件夹对话框IMO更简单的方法:

    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
Run Code Online (Sandbox Code Playgroud)