我正在设计一个小型的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?


当前回答

这适用于。net 5 (Windows):

 ProcessStartInfo psi = new ProcessStartInfo {
   FileName = "cmd.exe",
     Arguments = $ "/C start https://stackoverflow.com/",
     WindowStyle = ProcessWindowStyle.Hidden,
     CreateNoWindow = true
 };
 Process.Start(psi);

其他回答

在UWP:

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

看一下GeckoFX控件。

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

你试过这里提到的Processas: 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);
}

解决Net 6的问题 我从ChromeLauncher使用这个代码 ,默认浏览器会喜欢它

internal static class ChromeLauncher
{
    private const string ChromeAppKey = @"\Software\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe";

    private static string ChromeAppFileName
    {
        get
        {
            return (string) (Registry.GetValue("HKEY_LOCAL_MACHINE" + ChromeAppKey, "", null) ??
                                Registry.GetValue("HKEY_CURRENT_USER" + ChromeAppKey, "", null));
        }
    }

    public static void OpenLink(string url)
    {
        string chromeAppFileName = ChromeAppFileName;
        if (string.IsNullOrEmpty(chromeAppFileName))
        {
            throw new Exception("Could not find chrome.exe!");
        }
        Process.Start(chromeAppFileName, url);
    }
}

我想对上面的一个答案发表评论,但我还没有找到代表。

System.Diagnostics.Process.Start("explorer", "stackoverflow.com");

几乎工作,除非url有一个查询字符串,在这种情况下,这段代码只是打开一个文件资源管理器窗口。关键似乎是UseShellExecute标志,正如Alex Vang上面给出的答案(对其他关于在web浏览器中启动随机字符串的评论取模)。