我正在设计一个小型的c#应用程序,其中有一个web浏览器。我目前有我的电脑上所有的默认说谷歌chrome是我的默认浏览器,但当我点击我的应用程序中的一个链接打开一个新窗口,它打开ie浏览器。有没有办法让这些链接在默认浏览器中打开呢?还是我的电脑出了问题?

我的问题是,我在应用程序中有一个网络浏览器,所以说你去谷歌,输入“堆栈溢出”,右键单击第一个链接,单击“在新窗口中打开”,它在IE中打开,而不是Chrome。这是我编码不正确的东西,还是我的计算机上有一个设置不正确

= = = = = =进行编辑

This is really annoying. I am already aware that the browser is IE, but I had it working fine before. When I clicked a link it opened in chrome. I was using sharp develop to make the application at that time because I could not get c# express to start up. I did a fresh windows install and since I wasn't too far along in my application, I decided to start over, and now I am having this problem. That is why I am not sure if it is my computer or not. Why would IE start up the whole browser when a link is clicked rather than simply opening the new link in the default browser?


当前回答

我是唯一一个害怕调用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;
            }
        }

这段代码被设计用于CefSharp,但应该很容易适应。

其他回答

用当前版本的浏览器更新注册表 @“软件\ \ \ Microsoft Internet Explorer主要\ 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);

对于那些发现这个问题的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(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;
        }
    }
}

看一下GeckoFX控件。

GeckoFX是一个开源组件 这使得嵌入Mozilla很容易 Gecko (Firefox)到任何。net Windows 表单应用程序。写得很干净, 完全注释的c#, GeckoFX是 完美的替换默认 基于Internet explorer的WebBrowser 控制。

这为我打开了默认设置:

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

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

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

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