首页 文章

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

提问于
浏览
201

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

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

===编辑===

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

10 回答

  • 0

    你可以写

    System.Diagnostics.Process.Start("http://google.com");
    

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

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

  • 12

    这为我打开了默认值:

    System.Diagnostics.Process.Start(e.LinkText.ToString());
    
  • 29

    使用当前版本的资源管理器更新注册表
    @ "Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION"

    public enum BrowserEmulationVersion
    {
        Default = 0,
        Version7 = 7000,
        Version8 = 8000,
        Version8Standards = 8888,
        Version9 = 9000,
        Version9Standards = 9999,
        Version10 = 10000,
        Version10Standards = 10001,
        Version11 = 11000,
        Version11Edge = 11001
    }
    
    key.SetValue(programName, (int)browserEmulationVersion, RegistryValueKind.DWord);
    
  • 418

    在UWP中:

    await Launcher.LaunchUriAsync(new Uri("http://google.com"));
    
  • 0

    您是否尝试过这里提到的 Processhttp://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);
    }
    
  • 19

    看看GeckoFX control .

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

  • 0
    public static void GoToSite(string url)
    {
         System.Diagnostics.Process.Start(url);
    }
    

    这应该可以解决你的问题

  • 3

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

    码:

    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;
            }
        }
    }
    
  • 0

    动态打开

    string addres= "Print/" + Id + ".htm";
               System.Diagnostics.Process.Start(Path.Combine(Environment.CurrentDirectory, addres));
    
  • 5

    试试这个,老派的方式;)

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

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

相关问题