首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在文件夹浏览器对话框中排除共享打印机?

如何在文件夹浏览器对话框中排除共享打印机?
EN

Stack Overflow用户
提问于 2013-06-10 14:01:02
回答 1查看 568关注 0票数 3

在我的应用程序中,我只想显示“网络位置”,所以我使用了以下代码:

代码语言:javascript
复制
 Dim fbd As New FolderBrowserDialog
 fbd.ShowNewFolderButton = False
 Dim type As Type = fbd.[GetType]
 Dim fieldInfo As Reflection.FieldInfo = type.GetField("rootFolder", _
 Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
 '========= Now set the value for Folder Dialog using DirectCast
 '=== 18 = Network Neighborhood is the root
 fieldInfo.SetValue(fbd, DirectCast(18, Environment.SpecialFolder))
 If fbd.ShowDialog() = DialogResult.OK Then
      txtNetworkDrive.Text = fbd.SelectedPath
 End If

在这种情况下,还会显示“共享打印机”。

如果用户选择“打印机”,它会显示一个错误。

如何阻止“共享打印机”选项而只显示“共享文件夹”?

EN

回答 1

Stack Overflow用户

发布于 2013-06-12 03:23:30

您可以下载Hans建议的here的工作示例。

为了方便起见,这里有一个精简版,它使用了一个带有按钮和字段的简单表单,但也包含了额外的标志及其描述。

代码语言:javascript
复制
Imports System.Runtime.InteropServices

Public Class FoldersOnly


    <Flags()> _
    Public Enum BrowseInfoFlags As UInteger
        ''' <summary>
        ''' Only return file system directories. 
        ''' </summary>
        BIF_RETURNONLYFSDIRS = &H1
        ''' <summary>
        ''' Do not include network folders below the domain level in the dialog box's tree view control.
        ''' </summary>
        BIF_DONTGOBELOWDOMAIN = &H2
        ''' <summary>
        ''' Include a status area in the dialog box. 
        ''' The callback function can set the status text by sending messages to the dialog box. 
        ''' This flag is not supported when <bold>BIF_NEWDIALOGSTYLE</bold> is specified
        ''' </summary>
        BIF_STATUSTEXT = &H4
        ''' <summary>
        ''' Only return file system ancestors. 
        ''' An ancestor is a subfolder that is beneath the root folder in the namespace hierarchy. 
        ''' If the user selects an ancestor of the root folder that is not part of the file system, the OK button is grayed
        ''' </summary>
        BIF_RETURNFSANCESTORS = &H8
        ''' <summary>
        ''' Include an edit control in the browse dialog box that allows the user to type the name of an item.
        ''' </summary>
        BIF_EDITBOX = &H10
        ''' <summary>
        ''' If the user types an invalid name into the edit box, the browse dialog box calls the application's BrowseCallbackProc with the BFFM_VALIDATEFAILED message. 
        ''' This flag is ignored if <bold>BIF_EDITBOX</bold> is not specified.
        ''' </summary>
        BIF_VALIDATE = &H20
        ''' <summary>
        ''' Use the new user interface. 
        ''' Setting this flag provides the user with a larger dialog box that can be resized. 
        ''' The dialog box has several new capabilities, including: drag-and-drop capability within the 
        ''' dialog box, reordering, shortcut menus, new folders, delete, and other shortcut menu commands. 
        ''' </summary>
        BIF_NEWDIALOGSTYLE = &H40
        ''' <summary>
        ''' The browse dialog box can display URLs. The <bold>BIF_USENEWUI</bold> and <bold>BIF_BROWSEINCLUDEFILES</bold> flags must also be set. 
        ''' If any of these three flags are not set, the browser dialog box rejects URLs.
        ''' </summary>
        BIF_BROWSEINCLUDEURLS = &H80
        ''' <summary>
        ''' Use the new user interface, including an edit box. This flag is equivalent to <bold>BIF_EDITBOX | BIF_NEWDIALOGSTYLE</bold>
        ''' </summary>
        BIF_USENEWUI = (BIF_EDITBOX Or BIF_NEWDIALOGSTYLE)
        ''' <summary>
        ''' hen combined with <bold>BIF_NEWDIALOGSTYLE</bold>, adds a usage hint to the dialog box, in place of the edit box. <bold>BIF_EDITBOX</bold> overrides this flag.
        ''' </summary>
        BIF_UAHINT = &H100
        ''' <summary>
        ''' Do not include the New Folder button in the browse dialog box.
        ''' </summary>
        BIF_NONEWFOLDERBUTTON = &H200
        ''' <summary>
        ''' When the selected item is a shortcut, return the PIDL of the shortcut itself rather than its target.
        ''' </summary>
        BIF_NOTRANSLATETARGETS = &H400
        ''' <summary>
        ''' Only return computers. If the user selects anything other than a computer, the OK button is grayed.
        ''' </summary>
        BIF_BROWSEFORCOMPUTER = &H1000
        ''' <summary>
        ''' Only allow the selection of printers. If the user selects anything other than a printer, the OK button is grayed
        ''' </summary>
        BIF_BROWSEFORPRINTER = &H2000
        ''' <summary>
        ''' The browse dialog box displays files as well as folders.
        ''' </summary>
        BIF_BROWSEINCLUDEFILES = &H4000
        ''' <summary>
        ''' The browse dialog box can display shareable resources on remote systems.
        ''' </summary>
        BIF_SHAREABLE = &H8000
        ''' <summary>
        ''' Allow folder junctions such as a library or a compressed file with a .zip file name extension to be browsed.
        ''' </summary>
        BIF_BROWSEFILEJUNCTIONS = &H10000
    End Enum

    Private Structure BrowseInfo
        Public hWndOwner As IntPtr
        Public pidlRoot As Integer
        Public sDisplayName As String
        Public sTitle As String
        Public ulFlags As Integer
        Public lpfn As Integer
        Public lParam As Integer
        Public iImage As Integer
    End Structure



    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal nPidl As Integer, ByVal pszPath As String) As Integer
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (ByRef lpBrowseInfo As BrowseInfo) As Integer
    Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal PV_Renamed As Integer)

    Private Const BIF_RETURNONLYFSDIRS As Short = &H1S
    Private Const BIF_DONTGOBELOWDOMAIN As Short = &H2S
    Private Const BIF_STATUSTEXT As Short = &H4S
    Private Const BIF_RETURNFSANCESTORS As Short = &H8S
    Private Const BIF_BROWSEFORCOMPUTER As Short = &H1000S
    Private Const BIF_BROWSEFORPRINTER As Short = &H2000S
    Private Const MAX_PATH As Short = 256

    ' Let the user browse for a folder.
    ' Return the folder or Nothing if the user cancels.
    Public Function BrowseForFolder(ByRef dialog_title As _
        String, ByRef frm As Form) As String
        Try
            Dim browse_info As BrowseInfo
            With browse_info
                .hWndOwner = frm.Handle
                .pidlRoot = 0
                .sDisplayName = Space$(260)
                .sTitle = dialog_title
                .ulFlags = BrowseInfoFlags.BIF_RETURNONLYFSDIRS
                .lpfn = 0
                .lParam = 0
                .iImage = 0
            End With

            Dim item As Integer = _
                SHBrowseForFolder(browse_info)

            Dim path As String = New String(" "c, MAX_PATH)
            If SHGetPathFromIDList(item, path) = 0 Then
                path = Nothing
            Else
                path = path.Trim
            End If

            CoTaskMemFree(item)
            Return path
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return Nothing
        End Try
    End Function


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        txtNetworkDrive.Text = BrowseForFolder("test", Me)
    End Sub

End Class
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17017668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档