简体   繁体   English

使用 c# 和 Process.Start 打开默认浏览器 window

[英]Open a default browser window using c# and Process.Start

I am trying to open a web page using the default browser when someone hits an API endpoint.当有人点击 API 端点时,我正在尝试使用默认浏览器打开 web 页面。

I have this working on my local test machine:我在我的本地测试机器上工作:

    [HttpGet("Http/{classId}")]
    public void OpenWebLink(Guid classId)
    {
        string target = "http://astrolab.meeting.trl.edu/class/details.aspx?classId=" + classId;
        System.Diagnostics.Process.Start("C:\\Program Files\\Mozilla Firefox\\firefox.exe", target);
    }

But when I publish to a server that has IIS, it can't find firefox.exe但是当我发布到一个有 IIS 的服务器时,它找不到firefox.exe

The problem is, I had to put the full path to firefox just to get it to work on my machine.问题是,我必须将完整路径放到 firefox 才能让它在我的机器上运行。 If I didn't include the path like that I'd get this error:如果我不包含这样的路径,我会得到这个错误:

System.Diagnostics.Process.Start Win32Exception: 'The system cannot find the file specified.'

I also tried this:我也试过这个:

    [HttpGet("Http")]
    public void OpenWebLink(Guid classId)
    {
        try
        {

            var ps = new ProcessStartInfo("http://astrolab.meeting.trl.edu/class/details.aspx?classId=" + classId;)
            {
                Verb = "open"
            };
        Process.Start(ps);
        }
        catch (Win32Exception w32Ex) 
        {
            throw w32Ex;
        }
    }

But it still fails when I hit the endpoint on the IIS server with this:但是当我用这个访问 IIS 服务器上的端点时它仍然失败:

System.ComponentModel.Win32Exception (2): The system cannot find the file specified.

Is there a way to set it up so that it will find the default browser on any machine?有没有办法设置它以便它可以在任何机器上找到默认浏览器?

Thanks!谢谢!

Either use ShellExecute to launch your url (the more correct way), or pipe it through explorer (the lazier, less portable way).要么使用ShellExecute启动您的 url(更正确的方式),要么通过explorer (更懒惰、不太便携的方式)将其通过管道传输。

Process.Start(new()
{
    UseShellExecute = true,
    FileName = "http://google.ca",
});

Process.Start("explorer.exe", "http://google.ca");

It's as if do.net developers are unaware of other operating systems besides Windows...好像 do.net 开发人员不知道除了 Windows 之外还有其他操作系统......

@Blindy's answer only works on Windows, but nowhere did they indicate this. @Blindy 的回答仅适用于 Windows,但他们没有指出这一点。 And even if @SkyeBoniwell's question mentions IIS which implies Windows, neither the title of the question, nor the body, nor the tags explicitly mention Windows. As such, keeping in mind stackoverflow answers are meant to be used by everyone and not only the OP of a question, a correct answer to this thread should theoretically be os-agnostic.即使@SkyeBoniwell 的问题提到 IIS 这意味着 Windows,问题的标题、正文和标签都没有明确提到 Windows。因此,请记住,stackoverflow 的答案是供所有人使用的,而不仅仅是 OP一个问题,这个线程的正确答案理论上应该是操作系统不可知论者。 In practice, it should take into account as many operating systems as possible, ie at the very least the main three ones.实际上,它应该考虑尽可能多的操作系统,即至少是主要的三个操作系统。

Here is a solution that works for 99% of end-user systems:这是适用于 99% 的最终用户系统的解决方案:

    public static void OpenBrowser(string url)
    {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
        Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            Process.Start("xdg-open", url);
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
        {
            Process.Start("open", url);
        }
        else
        {
            // throw 
        }
    }

taken from here取自这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM