我无法在我的应用程序中使用 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 );
}
当应用程序运行并单击按钮并且我尝试打开文件时,会发生以下情况:
1st try:
它返回我的文件的路径,但不是
c:\dira\dirb\dirc\filename.ext
我有
c:\dira\dirb\dircfilename.ext
文件名前没有“\”
2nd try
一切都好
next:存在随机崩溃,例如随机访问冲突,或 GUI 冻结,并且即使在任务管理器中也无法终止应用程序进程,或其他错误。
通常,对话框会工作 2-3 次,然后应用程序就会永久崩溃。
我的代码有什么问题吗?
EDIT:
我无法使用 OpenFileDialog。我正在使用 WinPE 4.0(Windows 评估和部署工具包 ADK)。当我尝试 OpenFileDIalog 时,它会抛出运行时错误80040111 http://classicasp.aspfaq.com/components/why-do-i-get-80040111-errors.html。可能是因为核心不支持(就像Server Core不支持OpenFileDialog,错误是一样的)。可能在 WinPE 4.0 上,他们在记事本等应用程序中使用 GetOpenFileName。这对他们有用。