33

是否有一种“简单”的方法可以从同一个对话框中选择文件或文件夹?

在我创建的许多应用程序中,我允许文件或文件夹作为输入。到目前为止,我总是最终创建一个开关来在文件或文件夹选择对话框之间切换,或者只使用拖放功能。

由于这似乎是一个基本的东西,我想这是以前创建的,但谷歌搜索不会产生太多信息。所以看起来我需要从头开始并创建一个自定义选择对话框,但我宁愿不通过重新发明轮子来完成这样一个微不足道的任务来引入任何问题。

有人有任何提示或现有解决方案吗?

为了使 UI 保持一致,最好可以扩展 OpenFileDialog(或 FolderBrowserDialog)。

4

10 回答 10

13

从技术上讲,这是可能的。FolderBrowseDialog 使用的 shell 对话框能够返回文件和文件夹。不幸的是,该功能并未在 .NET 中公开。甚至反射也不能戳出所需的选项标志。

要使其工作,您必须 P/Invoke SHBrowseForFolder() 并在 BROWSEINFO.ulFlags 中打开 BIF_BROWSEINCLUDEFILES 标志(值 = 0x4000)。P/Invoke 是坚韧不拔的,最好在Reflector 的帮助下从另一个源或 FolderBrowseDialog 类本身复制和粘贴代码。

于 2009-01-10T15:01:58.597 回答
7

基于上述提示,我在以下位置找到了一些使用标准文件夹浏览器对话框的工作代码:http: //topic.csdn.net/t/20020703/05/845468.html

扩展文件夹浏览器对话框的类

Imports System   
Imports System.Text   
Imports System.Windows.Forms   
Imports System.Runtime.InteropServices   

Public Class DirectoryDialog 
    Public Structure BROWSEINFO 
        Public hWndOwner As IntPtr 
        Public pIDLRoot As Integer 
        Public pszDisplayName As String 
        Public lpszTitle As String 
        Public ulFlags As Integer 
        Public lpfnCallback As Integer 
        Public lParam As Integer 
        Public iImage As Integer 
    End Structure 

    Const MAX_PATH As Integer = 260

    Public Enum BrowseForTypes As Integer 
        Computers = 4096 
        Directories = 1 
        FilesAndDirectories = 16384 
        FileSystemAncestors = 8 
    End Enum 

    Declare Function CoTaskMemFree Lib "ole32" Alias "CoTaskMemFree" (ByVal hMem As IntPtr) As Integer 
    Declare Function lstrcat Lib "kernel32" Alias "lstrcat" (ByVal lpString1 As String, ByVal lpString2 As String) As IntPtr 
    Declare Function SHBrowseForFolder Lib "shell32" Alias "SHBrowseForFolder" (ByRef lpbi As BROWSEINFO) As IntPtr 
    Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDList" (ByVal pidList As IntPtr, ByVal lpBuffer As StringBuilder) As Integer 
    Protected Function RunDialog(ByVal hWndOwner As IntPtr) As Boolean 

        Dim udtBI As BROWSEINFO = New BROWSEINFO() 
        Dim lpIDList As IntPtr 
        Dim hTitle As GCHandle = GCHandle.Alloc(Title, GCHandleType.Pinned) 
        udtBI.hWndOwner = hWndOwner 
        udtBI.lpszTitle = Title 
        udtBI.ulFlags = BrowseFor 
        Dim buffer As StringBuilder = New StringBuilder(MAX_PATH) 
        buffer.Length = MAX_PATH 
        udtBI.pszDisplayName = buffer.ToString() 
        lpIDList = SHBrowseForFolder(udtBI) 
        hTitle.Free() 
        If lpIDList.ToInt64() <> 0 Then 
            If BrowseFor = BrowseForTypes.Computers Then 
                m_Selected = udtBI.pszDisplayName.Trim() 
            Else 
                Dim path As StringBuilder = New StringBuilder(MAX_PATH) 
                SHGetPathFromIDList(lpIDList, path) 
                m_Selected = path.ToString() 
            End If 
            CoTaskMemFree(lpIDList) 
        Else 
            Return False 
        End If 
        Return True 
    End Function 

    Public Function ShowDialog() As DialogResult 
        Return ShowDialog(Nothing) 
    End Function 

    Public Function ShowDialog(ByVal owner As IWin32Window) As DialogResult 
        Dim handle As IntPtr 
        If Not owner Is Nothing Then 
            handle = owner.Handle 
        Else 
            handle = IntPtr.Zero 
        End If 
        If RunDialog(handle) Then 
            Return DialogResult.OK 
        Else 
            Return DialogResult.Cancel 
        End If 
    End Function 

    Public Property Title() As String 
        Get 
            Return m_Title 
        End Get 
        Set(ByVal Value As String) 
            If Value Is DBNull.Value Then 
                Throw New ArgumentNullException() 
            End If 
            m_Title = Value 
        End Set 
    End Property

    Public ReadOnly Property Selected() As String 
        Get 
            Return m_Selected 
        End Get 
    End Property 

    Public Property BrowseFor() As BrowseForTypes
        Get 
            Return m_BrowseFor 
        End Get 
        Set(ByVal Value As BrowseForTypes) 
            m_BrowseFor = Value 
        End Set 
    End Property 

    Private m_BrowseFor As BrowseForTypes = BrowseForTypes.Directories 
    Private m_Title As String = "" 
    Private m_Selected As String = "" 

    Public Sub New() 
    End Sub
End Class 

实现扩展对话框的代码

Sub Button1Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim frmd As DirectoryDialog = New DirectoryDialog()
    ' frmd.BrowseFor = DirectoryDialog.BrowseForTypes.Directories   
    ' frmd.BrowseFor = DirectoryDialog.BrowseForTypes.Computers   
    frmd.BrowseFor = DirectoryDialog.BrowseForTypes.FilesAndDirectories   
    frmd.Title = "Select a file or a folder"    
    If frmd.ShowDialog(Me) = DialogResult.OK Then   
        MsgBox(frmd.Selected)   
    End If   
End Sub
于 2009-01-10T15:47:02.110 回答
5

它已经完成了。您可以使用FolderBrowserDialogEx - 内置 FolderBrowserDialog 的可重用衍生产品。这个允许您输入路径,甚至是 UNC 路径。您可以浏览文件夹或文件+文件夹。您可以使用它浏览计算机或打印机。基于内置 FBD,但……更好。更灵活。如果单击 GUI 中的文件夹,则路径会出现在文本框中。如果您键入路径,文件夹将被激活。内置对话框缺少很多选项。

完整的源代码。自由。MS-公共许可证。

FolderBrowserDialogEx

使用它的代码:

     var dlg1 = new Ionic.Utils.FolderBrowserDialogEx();
     dlg1.Description = "Select a folder to extract to:";
     dlg1.ShowNewFolderButton = true;
     dlg1.ShowEditBox = true;
     //dlg1.NewStyle = false;
     dlg1.SelectedPath = txtExtractDirectory.Text;
     dlg1.ShowFullPathInEditBox = true;
     dlg1.RootFolder = System.Environment.SpecialFolder.MyComputer;

     // Show the FolderBrowserDialog.
     DialogResult result = dlg1.ShowDialog();
     if (result == DialogResult.OK)
     {
         txtExtractDirectory.Text = dlg1.SelectedPath;
     }
于 2009-03-09T07:20:08.390 回答
5

您可以使用标准 OpenFileDialog 来选择一个文件夹。这是 CodeProject 中的一篇文章,它展示了一种方法(http://www.codeproject.com/KB/dialog/OpenFileOrFolderDialog.aspx)。

于 2009-11-25T20:54:23.127 回答
1

AFAIK,.NET 框架中没有任何东西可以开箱即用。

.NET 文件对话框派生自CommonDialog

继承类需要通过调用 ShowDialog 来实现 RunDialog 以创建特定的公共对话框。继承的类可以重写 HookProc 以实现特定的对话框挂钩功能。

于 2009-01-10T01:10:37.013 回答
1

所有内置对话框都使用与其操作相对应的 shell API,PrintDialog、OpenFileDialog、SaveFileDialog 等...

您很可能必须手动构建此功能。

于 2009-01-10T01:21:53.027 回答
1

http://www.pinvoke.net/default.aspx/shell32.shbrowseforfolder

如果您在此示例中进行更改,这里是 gerat 链接

  bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_SHAREABLE;

为了

  bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_SHAREABLE | BIF_BROWSEINCLUDEFILES;

你会得到你想要的

于 2014-09-09T06:40:00.583 回答
0

如果您只想显示特定的文件类型,以下文章(带有 C# 源代码)可以帮助您:

http://www.codeproject.com/KB/shell/csdoesshell1.aspx?fid=14137&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26

它还解释了可用于“自定义”FolderBrowser 对话框的其他选项,

于 2009-01-14T06:42:35.653 回答
0

这将允许您使用 OpenFileDialog 选择文件夹

        openFileDialog1.CheckFileExists = false;
        openFileDialog1.ValidateNames = false;
于 2016-01-18T04:22:15.820 回答
0

Ookii对话框实现了一个文件夹浏览器对话框,允许文件或文件夹作为输入,并且可用于 Windows 窗体和 WPF。

在此处输入图像描述

Ookii.Dialogs.Wpf

https://github.com/augustoproiete/ookii-dialogs-wpf


Ookii.Dialogs.WinForms

https://github.com/augustoproiete/ookii-dialogs-winforms

于 2018-10-20T21:40:47.920 回答