设置FolderBrowser的根文件夹

时间:2015-09-03 08:22:31

标签: 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

错误消息

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.

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

4 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

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

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

通过在线代码转换器(将其更改为VB .NET)运行它可能会很痛苦。这个文件夹浏览器比常规浏览器好很多。

答案 2 :(得分:0)

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

我使用以下函数返回所选文件夹的值...

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

答案 3 :(得分:0)

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

enter image description here