【发布时间】:2023-03-08 22:50:02
【问题描述】:
我找到了这个链接: C# Launch default browser with a default search query
但使用 FireFox 作为我的默认浏览器时,它会尝试在所选答案的引号中找到一个名为的文件。
代码;
//ToolStripMenu Click event to launch default browser and search internet for the value in a particular cell
private void tsmSearch_Click(object sender, EventArgs e)
{
int key = mp.GetRowAt(gdcErrorLogDefaultView, rowX, rowY);
if (key < 0)
return;
string ex = gdcErrorLogDefaultView.GetRowCellValue(key, "Exception").ToString();
string name = GetDefaultBrowser();
Process.Start(name, "\"?" + ex + "\"");
}
//Gets default browser from registry
private string GetDefaultBrowser()
{
string name;
RegistryKey regKey = null;
try
{
//set the registry key we want to open
regKey = Registry.ClassesRoot.OpenSubKey("HTTP\\shell\\open\\command", false);
//get rid of the enclosing quotes
name = regKey.GetValue(null).ToString().ToLower().Replace("" + (char)34, "");
//check to see if the value ends with .exe (this way we can remove any command line arguments)
if (!name.EndsWith("exe"))
//get rid of all command line arguments (anything after the .exe must go)
name = name.Substring(0, name.LastIndexOf(".exe") + 4);
}
catch (Exception ex)
{
name = string.Format("ERROR: An exception of type: {0} occurred in method: {1} in the following module: {2}", ex.GetType(), ex.TargetSite, this.GetType());
}
finally
{
//check and see if the key is still open, if so
//then close it
if (regKey != null)
regKey.Close();
}
return name;
}
我昨天在 StackOverflow 的某个地方找到了 GetDefaultBrowser() 代码,但现在找不到链接。奇怪的是,我将 Chrome 设置为默认浏览器,但该注册表项仍然显示为 FireFox。
有没有更简单的方法来启动默认浏览器并使用其默认搜索提供程序来查找术语?
【问题讨论】:
-
Windows 8 之前的系统不知道“默认搜索引擎提供商”。因此,无法使用默认提供程序加载默认浏览器。你的目标是什么操作系统?
-
@Gusdor 应用程序将主要在 Windows 7 上运行,但有些用户拥有 Vista 甚至 XP。我怀疑我们是否会在很长一段时间内看到任何运行 8 的东西。
-
那么恐怕您的要求是不可能的。您可以检测默认浏览器,但让该应用程序使用标准进行搜索的方法因浏览器而异。 stackoverflow.com/questions/2177957/…
-
我应该补充一点,firefox 使用这个命令行来处理 http 请求。
"C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -osint -url "%1"。您可以看到-url "%1"开关从命令行获取参数 1。 Google Chrome 的命令行允许您直接附加 URL,没有-url开关,但是 (!!!)... Firefox 也可能以这种方式运行。 IE浏览器?谁知道。都是未定义的。 -
嗯,如果我特别无聊,也许我会为三个浏览器写一个switch case,然后尝试做相应的过程?
标签: c# winforms default-browser