vb.net 为 FolderBrowser 设置根文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32370524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:26:40  来源:igfitidea点击:

Setting root folder for FolderBrowser

vb.netfolderbrowserdialog

提问by JefE

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?

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

回答by rheitzman

FolderBrowserDialog has always been a margin tool IMO.

FolderBrowserDialog 一直是 IMO 的边距工具。

When opening a standard mapped folder you can use RootFolderto remove some clutter. SelectedPathwill 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.

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

    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

在 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:

这是一种使用 的方法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

回答by Derek

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

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

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

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.

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

回答by DWE

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

德威

回答by CodeCatia

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

JefE,您问是否有办法使用除预定义特殊文件夹之外的其他根文件夹?你试过Shell.BrowseForFolder方法吗?

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

在此处输入图片说明