如何在C#中的默认浏览器中打开

Sea*_*ean 238 c# browser default window new-operator

我正在设计一个小型的C#应用​​程序,其中有一个Web浏览器.我目前在我的计算机上拥有所有默认值,表示谷歌浏览器是我的默认浏览器,但当我单击我的应用程序中的链接以在新窗口中打开时,它会打开Internet Explorer.有没有办法让这些链接在默认浏览器中打开?或者我的电脑有什么问题吗?

我的问题是我在应用程序中有一个webbrowser,所以说你去谷歌并输入"堆栈溢出"并右键单击第一个链接并单击"在新窗口中打开"它在IE而不是Chrome中打开.这是我编码不当的东西,还是我的电脑上的设置不正确

===编辑===

这真的很烦人.我已经知道浏览器是IE浏览器,但我以前工作正常.当我点击它在chrome中打开的链接时.我当时正在使用sharp develop来制作应用程序,因为我无法启动c#express.我做了一个全新的Windows安装,因为我在我的应用程序中并不太远,我决定重新开始,现在我遇到了这个问题.这就是为什么我不确定它是不是我的电脑.为什么IE会在点击链接时启动整个浏览器,而不是简单地在默认浏览器中打开新链接?

SLa*_*aks 475

你可以写

System.Diagnostics.Process.Start("http://google.com");
Run Code Online (Sandbox Code Playgroud)

编辑:WebBrowser控件是IE的嵌入式副本.
因此,它内部的任何链接都将在IE中打开.

要更改此行为,您可以处理该Navigating事件.

  • 不适用于 .NET Core。接受的答案应该支持 .NET Core,请参阅下面 Mayank Tripathi 的答案。 (8认同)
  • 请注意,此 can 方法也可能引入安全问题,因为如果“url”被替换为应用程序的物理路径,它也会执行 (5认同)
  • @Sean:是的.`的Process.Start(e.Url.ToString())` (3认同)
  • 尝试一下。使用Taskmgr.exe,您将看到*两个* iexporer.exe 副本正在运行。对于进程外的导航不会触发。 (2认同)
  • 除非将浏览器 exe 指定为第一个参数,否则本地 url (file:///) 不适用于查询字符串。 (2认同)

小智 37

public static void GoToSite(string url)
{
     System.Diagnostics.Process.Start(url);
}
Run Code Online (Sandbox Code Playgroud)

这应该可以解决你的问题

  • 应该是'静态虚空GotoSite' (7认同)
  • 无法在 .NET Core / .NET 5.0 中工作 (7认同)

Joe*_*kes 35

对于那些在dotnet核心中发现这个问题的人.我在这里找到了解决方案

码:

private void OpenUrl(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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 对于 Windows,我推荐 `Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true });` (8认同)
  • 这应该是最重要的答案,因为原始答案已经过时,并且仅在 Windows 上仍然相关。 (2认同)

May*_*thi 26

经过大量研究,我觉得大多数给定的答案不适用于 dotnet core。1 System.Diagnostics.Process.Start("http://google.com").; -- 不适用于 dotnet 核心

2.它会工作,但如果默认浏览器是chrome,它会阻止新窗口打开

 myProcess.StartInfo.UseShellExecute = true; 
    myProcess.StartInfo.FileName = "http://some.domain.tld/bla";
    myProcess.Start();
Run Code Online (Sandbox Code Playgroud)

下面是最简单的,适用于所有场景。

Process.Start("explorer", url);
Run Code Online (Sandbox Code Playgroud)


And*_*eas 14

您是否Process按照此处的说明进行了尝试:http://msdn.microsoft.com/de-de/library/system.diagnostics.process.aspx

你可以用

Process myProcess = new Process();

try
{
    // true is the default, but it is important not to set it to false
    myProcess.StartInfo.UseShellExecute = true; 
    myProcess.StartInfo.FileName = "http://some.domain.tld/bla";
    myProcess.Start();
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}
Run Code Online (Sandbox Code Playgroud)

  • @SLaks,谢谢.另一方面,重要的是要提到它必须是"真实的". (4认同)
  • 请注意 - .NET Core 不再将 `UseShellExecute` 默认为 true,因此该行是必需的。 (4认同)

Est*_*uis 9

我的默认浏览器是谷歌浏览器,接受的答案出现以下错误:

该系统找不到指定的文件。

我解决了这个问题,并使用以下代码设法用默认浏览器打开了一个 URL:

System.Diagnostics.Process.Start("explorer.exe", "http://google.com");
Run Code Online (Sandbox Code Playgroud)

  • 最上面的答案确实不起作用,这个起作用了,谢谢。 (4认同)

Tra*_*lip 8

我是唯一一个因为害怕而不敢调用System.Diagnostics.Process.Start()刚从互联网上读到的字符串的人吗?

        public bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
        {
            Request = request;
            string url = Request.Url;
            
            if (Request.TransitionType != TransitionType.LinkClicked)
            {   // We are only changing the behavoir when someone clicks on a link.
                // Let the embedded browser handle this request itself.
                return false;
            }
            else
            {   // The user clicked on a link.  Something like a filter icon, which links to the help for that filter.
                // We open a new window for that request.  This window cannot change.  It is running a JavaScript
                // application that is talking with the C# main program.
                Uri uri = new Uri(url);
                try
                {
                    switch (uri.Scheme)
                    {
                        case "http":
                        case "https":
                            {   // Stack overflow says that this next line is *the* way to open a URL in the
                                // default browser.  I don't trust it.  Seems like a potential security
                                // flaw to read a string from the network then run it from the shell.  This
                                // way I'm at least verifying that it is an http request and will start a
                                // browser.  The Uri object will also verify and sanitize the URL.
                                System.Diagnostics.Process.Start(uri.ToString());
                                break;
                            }
                        case "showdevtools":
                            {
                                WebBrowser.ShowDevTools();
                                break;
                            }
                    }
                }
                catch { }
                // Tell the browser to cancel the navigation.
                return true;
            }
        }
Run Code Online (Sandbox Code Playgroud)

该代码被设计为与 CefSharp 一起使用,但应该很容易适应。


THE*_*TOR 5

看看GeckoFX控件.

GeckoFX是一个开源组件,可以很容易地将Mozilla Gecko(Firefox)嵌入到任何.NET Windows Forms应用程序中.GeckoFX是用干净的,完全注释的C#编写的,是基于Internet Explorer的默认WebBrowser控件的完美替代品.


小智 5

尝试这个,老派的方法;)

public static void openit(string x)
    {
        System.Diagnostics.Process.Start("cmd", "/C start" + " " + x);
    }
Run Code Online (Sandbox Code Playgroud)

使用:openit(“ www.google.com”);

  • @Ben取决于输入来自何处。如果它是共享数据源,则一旦用户可以输入恶意命令,所有其他单击“转到”的用户都将受到该用户的摆布。 (4认同)
  • 这样的外壳,“ Shellsock”不能被利用吗? (2认同)

Ale*_*ang 5

我在 .NET 5、Windows 和 Windows 窗体中使用它。它甚至适用于其他默认浏览器(例如 Firefox):

Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true });
Run Code Online (Sandbox Code Playgroud)

基于这个这个