使用GetOpenFileName而不是OpenFileDialog

4
我无法在我的应用程序中使用OpenFileDialog。
作为替代,我使用GetOpenFileName()方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Reader
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public class OpenFileName
    {
        public int lstructSize;
        public IntPtr hwndOwner;
        public IntPtr hInstance;
        public string lpstrFilter = null;
        public string lpstrCustomFilter = null;
        public int lMaxCustomFilter;
        public int lFilterIndex;
        public string lpstrFile = null;
        public int lMaxFile = 0;
        public string lpstrFileTitle = null;
        public int lMaxFileTitle = 0;
        public string lpstrInitialDir = null;
        public string lpstrTitle = null;
        public int lFlags;
        public ushort nFileOffset;
        public ushort nFileExtension;
        public string lpstrDefExt = null;
        public int lCustData;
        public int lpfHook;
        public int lpTemplateName;
    }

    public class OpenDialog
    {
        [DllImport("Comdlg32.dll",CharSet = CharSet.Auto)]
        public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
    }
}

然后在按钮的OnClick事件中使用它,如下所示:

OpenFileName qofn = new OpenFileName();

qofn.lstructSize = Marshal.SizeOf(qofn);
qofn.lpstrFile = "";
qofn.lMaxFile = 256;
qofn.lpstrFileTitle = "";
qofn.lMaxFileTitle = 64;
qofn.hInstance = this.Handle;
source.Text = "Wait...";
if (OpenDialog.GetOpenFileName(qofn))
{
    MessageBox.Show("ofn.file: " + qofn. lpstrFile );
}

当应用程序运行并单击按钮并尝试打开文件时,会发生以下情况:
第一次尝试:它返回我的文件路径,但是在文件名之前没有'\',如下所示: c:\ dira \ dirb \ dircfilename.ext
第二次尝试:一切正常
接下来:出现随机崩溃,例如随机访问冲突或GUI冻结,并且即使在任务管理器中也无法杀死应用程序进程或其他错误。
通常,在应用程序彻底崩溃之前,对话框可以工作2-3次。
我的代码有什么问题?
编辑:

我无法使用 OpenFileDialog。我正在使用 WinPE 4.0(Windows 评估和部署工具包 ADK)。当我尝试使用 OpenFileDIalog 时,它会抛出运行时错误 80040111。这可能是因为核心不受支持(就像 Server Core 不支持 OpenFileDialog 一样,错误是相同的)。在 WinPE 4.0 上,它们在诸如记事本之类的应用程序中使用 GetOpenFileName。并且对他们有效。


3
有什么情况下 OpenFileDialog 无法工作? - M.Babcock
2
顺便说一下,this.Handle 不是 HINSTANCE,而是 HWND。 - Thomas Levesque
而是什么促使你以困难、容易出错、低级别的方式去做呢?并不是说没有好理由,只是想知道。 - MickeyfAgain_BeforeExitOfSO
1
最后三个字段是错误的,而且还有更多。您是否知道GetOpenFileName()已经完全过时了?现在已经被IFileDialog和相关工具所取代。 - Hans Passant
我编辑了我的帖子。我知道GetOpenFileName已经过时了。我无法使用OpenFileDialog或ComponentModel中的任何其他对话框,因为在没有资源管理器的系统上(例如Windows Server 2008或WinPE 4.0)会出现80040111运行时错误。 - Arie
显示剩余2条评论
2个回答

5

好的,我找到了这个微软的示例并且它可以使用:

// Copyright
// Microsoft Corporation
// All rights reserved

// OpenFileDlg.cs

using System;
using System.Text;
using System.Runtime.InteropServices;

/*
typedef struct tagOFN { 
  DWORD         lStructSize; 
  HWND          hwndOwner; 
  HINSTANCE     hInstance; 
  LPCTSTR       lpstrFilter; 
  LPTSTR        lpstrCustomFilter; 
  DWORD         nMaxCustFilter; 
  DWORD         nFilterIndex; 
  LPTSTR        lpstrFile; 
  DWORD         nMaxFile; 
  LPTSTR        lpstrFileTitle; 
  DWORD         nMaxFileTitle; 
  LPCTSTR       lpstrInitialDir; 
  LPCTSTR       lpstrTitle; 
  DWORD         Flags; 
  WORD          nFileOffset; 
  WORD          nFileExtension; 
  LPCTSTR       lpstrDefExt; 
  LPARAM        lCustData; 
  LPOFNHOOKPROC lpfnHook; 
  LPCTSTR       lpTemplateName; 
#if (_WIN32_WINNT >= 0x0500)
  void *        pvReserved;
  DWORD         dwReserved;
  DWORD         FlagsEx;
#endif // (_WIN32_WINNT >= 0x0500)
} OPENFILENAME, *LPOPENFILENAME; 
*/

[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Auto )]  
public class OpenFileName 
{
    public int      structSize = 0;
    public IntPtr   dlgOwner = IntPtr.Zero; 
    public IntPtr   instance = IntPtr.Zero;

    public String   filter = null;
    public String   customFilter = null;
    public int      maxCustFilter = 0;
    public int      filterIndex = 0;

    public String   file = null;
    public int      maxFile = 0;

    public String   fileTitle = null;
    public int      maxFileTitle = 0;

    public String   initialDir = null;

    public String   title = null;   

    public int      flags = 0; 
    public short    fileOffset = 0;
    public short    fileExtension = 0;

    public String   defExt = null; 

    public IntPtr   custData = IntPtr.Zero;  
    public IntPtr   hook = IntPtr.Zero;  

    public String   templateName = null; 

    public IntPtr   reservedPtr = IntPtr.Zero; 
    public int      reservedInt = 0;
    public int      flagsEx = 0;
}

public class LibWrap
{
    //BOOL GetOpenFileName(LPOPENFILENAME lpofn);

    [ DllImport( "Comdlg32.dll", CharSet=CharSet.Auto )]                
    public static extern bool GetOpenFileName([ In, Out ] OpenFileName ofn );   
}

public class App
{
    public static void Main()
    {
        OpenFileName ofn = new OpenFileName();

        ofn.structSize = Marshal.SizeOf( ofn );

        ofn.filter = "Log files\0*.log\0Batch files\0*.bat\0";

        ofn.file = new String( new char[ 256 ]);
        ofn.maxFile = ofn.file.Length;

        ofn.fileTitle = new String( new char[ 64 ]);
        ofn.maxFileTitle = ofn.fileTitle.Length;    

        ofn.initialDir = "C:\\";
        ofn.title = "Open file called using platform invoke...";
        ofn.defExt = "txt";

        if( LibWrap.GetOpenFileName( ofn ))
        {
            Console.WriteLine( "Selected file with full path: {0}", ofn.file );
            Console.WriteLine( "Selected file name: {0}", ofn.fileTitle );
            Console.WriteLine( "Offset from file name: {0}", ofn.fileOffset );
            Console.WriteLine( "Offset from file extension: {0}", ofn.fileExtension );
        }
    }
}

你有完整示例的URL吗?我找到的最接近的是这篇MSDN文章 - Uwe Keim

2
OpenFileDlg示例(如Arie答案中使用的示例)可以在此处找到。该页面有C#,C ++和VB示例。我已经在WinPE 5.1中测试了VB示例,并且除了文件类型过滤器之外,它非常有效;它始终显示所有文件类型。让我惊讶的是,即使在我的WinPE映像中安装了WinPE-NetFX软件包,OpenFileDialog也不可用于WinPE,尝试使用它会引发问题中提到的错误。这些使用未管理的GetOpenFileName函数的OpenFileDlg示例是我在WinPE中能够找到的唯一替代方案。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接