Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Process.Start(“microsoft edge:”microsoft edge:)在dot net core中引发Win32Exception_C#_.net Core_Process_Microsoft Edge - Fatal编程技术网

C# Process.Start(“microsoft edge:”microsoft edge:)在dot net core中引发Win32Exception

C# Process.Start(“microsoft edge:”microsoft edge:)在dot net core中引发Win32Exception,c#,.net-core,process,microsoft-edge,C#,.net Core,Process,Microsoft Edge,当我执行以下代码行时: Process.Start("microsoft-edge:"); 或 它给了我这个错误: System.ComponentModel.Win32异常:“系统找不到指定的文件。” 当我使用Win+R运行microsoft edge:时,它会工作 当我在.net framework中运行相同的代码时,它可以正常工作 我正在使用.netcore 3.0.0-preview6-27804-01 知道为什么会这样吗 编辑: 这些措施也不起作用: Process.Start(@

当我执行以下代码行时:

Process.Start("microsoft-edge:");

它给了我这个错误:

System.ComponentModel.Win32异常:“系统找不到指定的文件。”

当我使用Win+R运行microsoft edge:时,它会工作

当我在.net framework中运行相同的代码时,它可以正常工作

我正在使用
.netcore 3.0.0-preview6-27804-01

知道为什么会这样吗


编辑:

这些措施也不起作用:

Process.Start(@"c:\Windows\System32\LaunchWinApp.exe:http://localhost");
Process.Start(@"http://localhost");
我系统上的所有其他可执行文件都正常工作

这也很有效,但我无法用它打开特定网页:

Process.Start("explorer", @"shell:Appsfolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge");

Windows
Run
window(Win+R)知道如何将“
microsoft edge:
”解析为可执行文件的路径。当您通过
Run
窗口调用它时,它首先解析为路径。然后执行该路径中的实际可执行文件

使用
Process.Start
,不存在此路径解析。它只查找一些路径,如应用程序路径或
path
变量。显然,它找不到要运行的可执行文件,因此出现了错误

如果在系统中使用引号声明了path变量,则在启动在该位置找到的任何进程时,必须完全限定该路径。否则,系统将找不到路径。例如,如果c:\mypath不在您的路径中,并且您使用引号添加它:path=%path%;“c:\mypath”,启动时必须完全限定c:\mypath中的任何进程

请注意,此重载不允许使用命令行参数:

此重载不允许进程使用命令行参数。如果需要为流程指定一个或多个命令行参数,请使用process.Start(ProcessStartInfo)或process.Start(String,String)重载


您不能简单地使用预期的调用
Process.Start(“url”)打开url

您必须创建一个
ProcessStartInfo
,并将浏览器和url作为参数传递:

Process.Start(new ProcessStartInfo("cmd", $"/c start microsoft-edge:http://localhost") { CreateNoWindow = true });
(编辑)需要使用
ProcessStartInfo
,因为我们需要设置它的
CreateNoWindow=true
以防止出现cmd窗口

<> p>但是,这个代码不仅可以在Windows机器上运行,我也会考虑使用更多类似于跨平台的特定的东西():


OpenBrowser(“http://localhost");

此处指出了确切的根本原因:

“在.NET Framework应用程序上默认为true,.NET核心应用程序上默认为false。”

这意味着直接的解决方案是设置UseShellExecute。以下内容基本上与原始内容相同,只是创建了一个显式的Process对象来修改设置

       Process pWeb = new Process();
       pWeb.StartInfo.UseShellExecute = true;
       pWeb.StartInfo.FileName = "microsoft-edge:http://localhost";
       pWeb.Start();

要感谢@Lex Li和@Bizhan对这个解决方案的评论。

这个变体似乎也能起到作用:

Process.Start(new ProcessStartInfo("msedge") { 
  UseShellExecute = true, 
  Arguments = "http://localhost" });
它避免了
MicrosoftEdge:
语法的奇怪性,从而允许以常规方式传递更多信息


如果有人知道避免这样做的原因,请大胆说出来:)

wow!另一个不合理的否决票:-)遗憾的是,没有一个答案能说出确切的根本原因,“默认值在.NET Framework应用程序上为true,在.NET核心应用程序上为false。”谢谢@LexLi这意味着更好的解决方案是设置
UseShellExecute=true
非常感谢!这对我帮助很大。我们可以添加这个url=url.Replace(“&”、“^&”);在Process.Start之前(新的ProcessStartInfo(“cmd”,“$”/c Start{url}”){CreateNoWindow=true});因为cmd.exe会将^识别为&。
       Process pWeb = new Process();
       pWeb.StartInfo.UseShellExecute = true;
       pWeb.StartInfo.FileName = "microsoft-edge:http://localhost";
       pWeb.Start();
Process.Start(new ProcessStartInfo("msedge") { 
  UseShellExecute = true, 
  Arguments = "http://localhost" });