gpt4 book ai didi

c# - Process.Start ("microsoft-edge:") 在 dot net core 中抛出 Win32Exception

转载 作者:行者123 更新时间:2023-11-30 15:51:32 29 4
gpt4 key购买 nike

当我执行下面这行代码时:

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

或者

Process.Start("microsoft-edge:http://localhost");

它给我这个错误:

System.ComponentModel.Win32Exception: 'The system cannot find the file specified.'

当我使用 Win+R 运行 microsoft-edge: 时,它起作用了。

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

我正在使用 .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");

最佳答案

您不能简单地用预期的调用打开一个 url Process.Start("url");

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

Process.Start(new ProcessStartInfo("cmd", $"/c start microsoft-edge:http://localhost") { CreateNoWindow = true });

(edit) ProcessStartInfo 的使用是必需的,因为我们需要设置它的 CreateNoWindow = true 以防止 cmd 窗口显示

但由于这段代码不仅可以在 Windows 机器上运行,我会考虑使用像这样的更跨平台的东西(https://brockallen.com/2016/09/24/process-start-for-urls-on-net-core/):

public void OpenBrowser(string url)
{
try
{
Process.Start(url);
}
catch
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
throw;
}
}
}

然后用 OpenBrowser("http://localhost"); 调用它

关于c# - Process.Start ("microsoft-edge:") 在 dot net core 中抛出 Win32Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57057459/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com