【问题标题】:Process.Start with .NET Core 2.0 on LinuxProcess.Start 在 Linux 上使用 .NET Core 2.0
【发布时间】:2018-09-14 19:32:47
【问题描述】:

我正在尝试启动默认 Web 浏览器以打开带有 Process.Start() 的链接。我使用Process.Start("https://www.google.com"),但我的 .NET Core 应用程序崩溃并出现以下问题:

发生异常:CLR/System.ComponentModel.Win32Exception An “System.ComponentModel.Win32Exception”类型的未处理异常 System.Diagnostics.Process.dll 中发生:'没有这样的文件或 目录'在 System.Diagnostics.Process.ResolvePath(String 文件名)在 System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo) 在 System.Diagnostics.Process.Start() 在 System.Diagnostics.Process.Start(ProcessStartInfo startInfo) 在 VPGameHelper.Program.Main(String[] args) 在 /home/jj/VPGameHelper/Program.cs:第 30 行

【问题讨论】:

    标签: c# linux .net-core


    【解决方案1】:

    我认为这可能是您正在寻找的:

    Process.Start for URLs on .NET Core

    最初的问题被问到here 在哪里。

    public static void OpenBrowser(string url)
    {
        try
        {
            Process.Start(url);
        }
        catch
        {
            // hack because of this: https://github.com/dotnet/corefx/issues/10361
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                url = url.Replace("&", "^&");
                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;
            }
        }
    }
    

    干杯,编码愉快!

    【讨论】:

    • 谢谢。我需要确保并将我的网址括在 xdg-open 的引号中。
    猜你喜欢
    • 1970-01-01
    • 2020-04-05
    • 2018-01-22
    • 2019-06-12
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 2019-10-04
    • 2018-11-20
    相关资源
    最近更新 更多