为什么在控制台应用程序中使用 CommonOpenFileDialog 时会出现此异常?

Obs*_*rno 5 c# dialog openfiledialog windows-api-code-pack

问题

我正在尝试使用 CommonOpenFileDialog 的文件夹选择器,如本答案中所述。问题是,即使有一个非常精简的示例项目,我在尝试使用 CommonOpenFileDialog 的函数时也会遇到异常ShowDialog()

代码

using Microsoft.WindowsAPICodePack.Dialogs;

namespace DialogTest
{
    class Program
    {
        public static void Main(string[] args)
        {
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.InitialDirectory = "C:\\Users";
            dialog.IsFolderPicker = true;

            CommonFileDialogResult result = dialog.ShowDialog();

            if (result == CommonFileDialogResult.Ok)
            {
                //Do Stuff
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我还与作者 Microsoft 一起使用以下 nuget 包:

  • Microsoft.WindowsAPICodePack-核心
  • Microsoft.WindowsAPICodePack-Shell

例外情况

此代码在 处产生以下异常dialog.ShowDialog();

System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2147023116
  HResult=-2147023116
  Message=A null reference pointer was passed to the stub. (Exception from HRESULT: 0x800706F4)
  Source=Microsoft.WindowsAPICodePack.Shell
  StackTrace:
       at Microsoft.WindowsAPICodePack.Dialogs.IFileDialog.SetFileName(String pszName)
       at Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialog.ApplyNativeSettings(IFileDialog dialog) in c:\projects\Windows API Code Pack 1.1\source\WindowsAPICodePack-NuGet\Shell\CommonFileDialogs\CommonFileDialog.cs:line 768
       at Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialog.ShowDialog() in c:\projects\Windows API Code Pack 1.1\source\WindowsAPICodePack-NuGet\Shell\CommonFileDialogs\CommonFileDialog.cs:line 609
       at DialogTest.Program.Main(String[] args) in c:\users\obscerno\documents\visual studio 2015\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 13
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
Run Code Online (Sandbox Code Playgroud)

其他一些相关细节

  1. 我正在使用 Visual Studio 2015。
  2. 关于这个错误的一个奇怪的事情是这个代码在一年前是有效的。我刚刚重新打开项目计划进行一些小的更改,但它不再起作用。
  3. 在创建新的测试项目时,第一次运行时 Visual Studio 会提示我找到一个名为CommonFileDialog.cs.

    它检查文件的初始目录是“c:\projects\Windows API Code Pack 1.1\source\WindowsAPICodePack-NuGet\Shell\CommonFileDialogs\CommonFileDialog.cs”,该目录在我的计算机上不存在。

    如果我选择“取消”,则在以后的调试期间不会返回提示。我怀疑这个丢失的文件是问题的根源,但不知道如何处理这些信息。

我尝试过的

  1. 除了这个有趣但不相关的问题之外,搜索异常并没有发现太多任何东西。

  2. 从多个来源安装相同的 nuget 包并没有给我任何不同的结果。这些软件包有相当多的副本,因为微软有一段时间不提供它们

  3. 我尝试在计算机中搜索文件“CommonFileDialog.cs”,但找不到它。

Obs*_*rno 2

正如trykyn在评论中提到的[STAThread],解决这个问题的方法是在Main前面添加。

工作代码如下所示。请注意,它需要调用System.

using System;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace DialogTest
{
    class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.InitialDirectory = "C:\\Users";
            dialog.IsFolderPicker = true;

            CommonFileDialogResult result = dialog.ShowDialog();

            if (result == CommonFileDialogResult.Ok)
            {
                //Do Stuff
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有关 STAThread 是什么的更多信息,这个答案详细介绍了它:[STAThread] 做什么?

STAThreadAttribute 本质上是 Windows 消息泵与 COM 组件通信的要求。尽管核心 Windows 窗体不使用 COM,但操作系统的许多组件(例如系统对话框)确实使用了此技术。