我正在设计一个小型的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("https://google.com");

这适用于大多数情况下,但我遇到了一个问题,有一个指向文件的url:

系统无法找到指定的文件。

所以,我尝试了这个解决方案,只是做了一些修改:

System.Diagnostics.Process.Start("explorer.exe", $"\"{uri}\"");

不用“”包装url,资源管理器会打开您的文档文件夹。

其他回答

开放的动态

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

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

系统无法找到指定的文件。

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

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

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

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

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

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

这适用于。net 5 (Windows):

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