为 FolderBrowser 设置根文件夹

Setting root folder for FolderBrowser

如何设置文件夹对话框的根文件夹?

我的示例似乎不起作用。 (我检查过该文件夹存在)

    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

错误信息

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.

我需要做什么才能将我的自定义位置设置为根文件夹?

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

在 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

这里有一种使用 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

我建议使用 FolderBrowserDialogEx:FolderBrowserDialog 的 C# 自定义。

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

虽然通过在线代码翻译器 运行 它可能会很痛苦(将其更改为 VB .NET)。这个文件夹浏览器比普通的好很多。

遇到这个问题 - 不是这里的常客...但我想我可以 post 一些有用的东西,寻找类似答案的人可能会感激...

我使用以下函数 return 选定文件夹的值 ...

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

希望这对某人有帮助...

DWE

JefE,你问有没有办法使用另一个根文件夹而不是预定义的特殊文件夹?您尝试过 Shell.BrowseForFolder 方法吗?

试试这个:

    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