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


当前回答

在UWP:

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

其他回答

这适用于。net 5 (Windows):

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

开放的动态

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

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

您可以在默认浏览器中使用cmd命令start <link>打开一个链接,此方法适用于所有具有在cmd.exe上执行系统命令功能的语言。

这是我在。net 6中用来执行带有重定向输出和输入的系统命令的方法,也非常确定它在。net 5中经过一些修改后也能工作。

using System.Diagnostics.Process cmd = new();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();


cmd.StandardInput.WriteLine("start https://google.com"); 
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();

这为我打开了默认设置:

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