c# - 使用 GetOpenFileName 而不是 OpenFileDialog

标签 c# dllimport

我不能在我的应用程序中使用 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\dirc\filename.ext
我有
c:\dira\dirb\dircfilename.ext
文件名前没有“\”

第二次尝试

一切都好

下一个:
有随机崩溃,例如随机访问冲突,或 GUI 卡住,即使在任务管理器或其他错误中也无法终止应用程序的进程。

通常对话框在应用程序崩溃之前工作 2-3 次。

我的代码有什么问题?

编辑:

我不能使用 OpenFileDialog。我正在使用 WinPE 4.0(Windows 评估和部署工具包 ADK)。当我尝试 OpenFileDialog 时,它抛出运行时错误 80040111 .可能是因为不支持核心(就像Server Core不支持OpenFileDialog,错误一样)。可能在 WinPE 4.0 上,他们在记事本等应用程序中使用 GetOpenFileName。它对他们有用。

最佳答案

好的,我找到了这个微软示例并且它有效:

// 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 );
        }
    }
}

关于c# - 使用 GetOpenFileName 而不是 OpenFileDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9088227/

相关文章:

c - 如何使用 Visual Studio 2010 为 64 位编译 C DLL?

c# - Asp.Net C# Dll导入问题

C# 使用 httpclient 发出 https 请求

c# - Azure Blob 下载 (GET) 导致 PUT 请求失败

c# - 如何使用 C# 从 html 页面中抓取文本?

c# - 通过 DllImport 在 C# 中调用 C 方法 - 尝试读取或写入 protected 内存

c# - C# 中的 typedef 等效于使用 C++ DLL

C# 闭包,为什么循环变量是通过引用捕获的?

c# - 需要帮助在 WPF 中反序列化字典 Json

c# - 将 C 编写的 DLL 调用到 C# 时,vshost32-clr2.exe 出错