问题描述
我刚刚将桌面应用程序从Framework迁移到了Core 3.0.
I have just migrated a desktop app from Framework to Core 3.0.
Process.Start(path_to_folder);
在Framework中工作正常,但在Core中抛出 Win32Exception:Access拒绝
. Process.Start("explorer.exe",path_to_folder);
都可以正常工作.
Process.Start(path_to_folder);
workes fine in Framework but throws Win32Exception: Access denied
in the Core. Process.Start("explorer.exe", path_to_folder);
works fine on both.
这是Core中的错误或限制吗?
Is this a bug or limitation in the Core?
推荐答案
我怀疑 ProcessStartInfo.UseShellExecute
属性是允许它在.NET Framework上运行的原因.从文档中...
I suspect the ProcessStartInfo.UseShellExecute
property is what would allow this to work on .NET Framework. From the documentation...
...以及仅采用 string
参数的 Process.Start()
重载可能会将该属性保留为默认值.要解决此问题,请创建您自己的 ProcessStartInfo
,并将 UseShellExecute
属性设置为 true
,并将其传递给 Process.Start的重载.()
代替...
...and the Process.Start()
overloads that just take string
parameters will likely just leave that property at the default. To work around this, create your own ProcessStartInfo
with the UseShellExecute
property set to true
and pass that to an overload of Process.Start()
instead...
ProcessStartInfo startInfo = new ProcessStartInfo(path_to_folder) {
UseShellExecute = true
};
Process.Start(startInfo);
为完整性起见,当我尝试运行此代码时...
For completeness, when I try running this...
Process.Start(Environment.SystemDirectory);
...在.NET Core 3.0中,我收到此异常...
...in .NET Core 3.0 I get this exception...
Process.Start()
和 Process.StartWithCreateProcess(ProcessStartInfo startInfo)
之间缺少的链接是 Process.StartCore(ProcessStartInfo startInfo)
,它根据 UseShellExecute
的值进行分支,我想可以内联了.异常似乎是 CreateProcess()
,大概是因为目录路径被指定为可执行文件.
The missing link between Process.Start()
and Process.StartWithCreateProcess(ProcessStartInfo startInfo)
is Process.StartCore(ProcessStartInfo startInfo)
, which branches based on the value of UseShellExecute
and I imagine gets inlined. The exception appears to be thrown after a call to CreateProcess()
, presumably because a directory path is specified as the file to executable.
请注意,如果将不可执行文件的路径传递给相同的 Process.Start(String fileName)
重载,则异常消息将变为指定的可执行文件不是此操作系统的有效应用程序平台."
Note that if you pass a path to a non-executable file to that same Process.Start(String fileName)
overload the exception message becomes "The specified executable is not a valid application for this OS platform."
调用 Process.Start(String fileName,String arguments)
仍然有效,原因是 ProcessStartInfo
实例具有(即使
. UseShellExecute
为 false
仍可以直接执行的explorer.exe
The reason why calling 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 that does refer to a file (explorer.exe
) that can be directly executed even if UseShellExecute
is false
.
这篇关于Core.3.0中的Process.Start不会仅按其名称打开文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!