我想在计算机中打开默认浏览器,然后模拟单击按钮。我创建了一个新的控制台应用程序,如下所示:
class Program
{
[STAThread]
static void Main(string[] args)
{
var url = "http://google.com";
Process.Start(url);
Console.ReadKey();
}
}
现在我该怎么办?我发现WebBrowser
类,但我认为它适用于Windows窗体应用程序中的浏览器控件..
答案 0 :(得分:1)
使用以下功能获取默认浏览器
private static string GetStandardBrowserPath()
{
string browserPath = string.Empty;
RegistryKey browserKey = null;
try
{
//Read default browser path from Win XP registry key
browserKey = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
//If browser path wasn't found, try Win Vista (and newer) registry key
if (browserKey == null)
{
browserKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http", false); ;
}
//If browser path was found, clean it
if (browserKey != null)
{
//Remove quotation marks
browserPath = (browserKey.GetValue(null) as string).ToLower().Replace("\"", "");
//Cut off optional parameters
if (!browserPath.EndsWith("exe"))
{
browserPath = browserPath.Substring(0, browserPath.LastIndexOf(".exe") + 4);
}
//Close registry key
browserKey.Close();
}
}
catch
{
//Return empty string, if no path was found
return string.Empty;
}
//Return default browsers path
return browserPath;
}
在默认浏览器中打开网址:
string url = "http://google.com";
string browserPath = GetStandardBrowserPath();
if (string.IsNullOrEmpty(browserPath))
{
MessageBox.Show("No default browser found!");
}
else
{
Process.Start(browserPath, url);
}