Process.Start 在 Core 3.0 中不会仅通过名称打开文件夹

Process.Start in Core 3.0 does not open a folder just by its name

我刚刚将桌面应用程序从 Framework 迁移到 Core 3.0。

Process.Start(path_to_folder); 在框架中工作正常,但在核心中抛出 Win32Exception: Access deniedProcess.Start("explorer.exe", path_to_folder); 两者都很好。

这是 Core 中的错误或限制吗?

我怀疑 ProcessStartInfo.UseShellExecute property 是允许它在 .NET Framework 上运行的原因。从文档...

The default is true on .NET Framework apps and false on .NET Core apps.

...而仅采用 string 参数的 Process.Start() 重载可能会将 属性 保留为默认值。要解决此问题,请创建自己的 ProcessStartInfo 并将 UseShellExecute 属性 设置为 true 并将其传递给 Process.Start() 的重载...

ProcessStartInfo startInfo = new ProcessStartInfo(path_to_folder) {
    UseShellExecute = true
};

Process.Start(startInfo);

为了完整性,当我尝试 运行 这个...

Process.Start(Environment.SystemDirectory);

...在 .NET Core 3.0 中我得到这个异常...

Unhandled exception. System.ComponentModel.Win32Exception (5): Access is denied.

Process.Start()Process.StartWithCreateProcess(ProcessStartInfo startInfo) 之间缺少的 link 调用后是 Process.StartCore(ProcessStartInfo startInfo), which branches based on the value of UseShellExecute and I imagine gets inlined. The exception appears to be thrown CreateProcess(),大概是因为目录路径被指定为可执行文件。

请注意,如果您将不可执行文件的路径传递给相同的 Process.Start(String fileName) 重载,则异常消息将变为 "The specified executable is not a valid application for this OS platform."

之所以调用Process.Start(String fileName, String arguments) works despite following largely the same code path is because the ProcessStartInfo instance it creates under the covers has a FileName property引用了一个即使UseShellExecute也可以直接执行的文件(explorer.exe) false.