Process.Start("microsoft-edge:") 在 dot net core 中抛出 Win32Exception [英] Process.Start("microsoft-edge:") throws Win32Exception in dot net core

查看:31
本文介绍了Process.Start("microsoft-edge:") 在 dot net core 中抛出 Win32Exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行以下代码行时:

When I execute the following line of code:

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

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

它给了我这个错误:

System.ComponentModel.Win32Exception:系统找不到指定的文件."

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

当我使用 Win+R 运行 microsoft-edge: 时,它可以工作.

When I run microsoft-edge: using Win+R it works.

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

When I run the same code in .net framework it works.

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

知道为什么会这样吗?

这些也不起作用:

Process.Start(@"c:WindowsSystem32LaunchWinApp.exe:http://localhost");
Process.Start(@"http://localhost");

我系统上的所有其他可执行文件都可以工作.

All other executables on my system work.

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

Also this is working too but I can't open a specific webpage with it:

Process.Start("explorer", @"shell:AppsfolderMicrosoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge");

推荐答案

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

You cannot simply open a url with the expected call Process.Start("url");

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

You have to create a ProcessStartInfo and pass your browser and url as arguments:

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

(edit) 需要使用ProcessStartInfo,因为我们需要设置它的CreateNoWindow = true 以防止cmd窗口出现.

(edit) The use of ProcessStartInfo is required because we need to set its CreateNoWindow = true to prevent cmd window from showing up.

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

But as this code not only can be run on a Windows machine, i'd consider using something more cross-platform specific like this (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");

这篇关于Process.Start("microsoft-edge:") 在 dot net core 中抛出 Win32Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆