Sea*_*ean 238 c# browser default window new-operator
我正在设计一个小型的C#应用程序,其中有一个Web浏览器.我目前在我的计算机上拥有所有默认值,表示谷歌浏览器是我的默认浏览器,但当我单击我的应用程序中的链接以在新窗口中打开时,它会打开Internet Explorer.有没有办法让这些链接在默认浏览器中打开?或者我的电脑有什么问题吗?
我的问题是我在应用程序中有一个webbrowser,所以说你去谷歌并输入"堆栈溢出"并右键单击第一个链接并单击"在新窗口中打开"它在IE而不是Chrome中打开.这是我编码不当的东西,还是我的电脑上的设置不正确
===编辑===
这真的很烦人.我已经知道浏览器是IE浏览器,但我以前工作正常.当我点击它在chrome中打开的链接时.我当时正在使用sharp develop来制作应用程序,因为我无法启动c#express.我做了一个全新的Windows安装,因为我在我的应用程序中并不太远,我决定重新开始,现在我遇到了这个问题.这就是为什么我不确定它是不是我的电脑.为什么IE会在点击链接时启动整个浏览器,而不是简单地在默认浏览器中打开新链接?
SLa*_*aks 475
你可以写
System.Diagnostics.Process.Start("http://google.com");
Run Code Online (Sandbox Code Playgroud)
编辑:WebBrowser控件是IE的嵌入式副本.
因此,它内部的任何链接都将在IE中打开.
要更改此行为,您可以处理该Navigating事件.
小智 37
public static void GoToSite(string url)
{
System.Diagnostics.Process.Start(url);
}
Run Code Online (Sandbox Code Playgroud)
这应该可以解决你的问题
Joe*_*kes 35
对于那些在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("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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
May*_*thi 26
经过大量研究,我觉得大多数给定的答案不适用于 dotnet core。1 System.Diagnostics.Process.Start("http://google.com").; -- 不适用于 dotnet 核心
2.它会工作,但如果默认浏览器是chrome,它会阻止新窗口打开
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = "http://some.domain.tld/bla";
myProcess.Start();
Run Code Online (Sandbox Code Playgroud)
下面是最简单的,适用于所有场景。
Process.Start("explorer", url);
Run Code Online (Sandbox Code Playgroud)
And*_*eas 14
您是否Process按照此处的说明进行了尝试: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);
}
Run Code Online (Sandbox Code Playgroud)
我的默认浏览器是谷歌浏览器,接受的答案出现以下错误:
该系统找不到指定的文件。
我解决了这个问题,并使用以下代码设法用默认浏览器打开了一个 URL:
System.Diagnostics.Process.Start("explorer.exe", "http://google.com");
Run Code Online (Sandbox Code Playgroud)
我是唯一一个因为害怕而不敢调用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;
}
}
Run Code Online (Sandbox Code Playgroud)
该代码被设计为与 CefSharp 一起使用,但应该很容易适应。
小智 5
尝试这个,老派的方法;)
public static void openit(string x)
{
System.Diagnostics.Process.Start("cmd", "/C start" + " " + x);
}
Run Code Online (Sandbox Code Playgroud)
使用:openit(“ www.google.com”);
我在 .NET 5、Windows 和 Windows 窗体中使用它。它甚至适用于其他默认浏览器(例如 Firefox):
Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true });
Run Code Online (Sandbox Code Playgroud)