打开Chrome标签并关闭它

我想打开Goog​​le Chrome标签页或默认浏览器。 然后在用户选择之后关闭它。

我在用

Process.Start("HTTP://www.MySite.Com"); 

要打开浏览器,但我没有关闭它的句柄。 我也不想关闭整个浏览器,只关闭我打开的标签。

这在Firefox中适用于我:

 var proc = Process.Start("firefox.exe", "http://www.google.nl"); proc.Kill(); 

因为我将Firefox设置为单窗口模式,所以它会打开一个选项卡。 当我发出Kill()方法时,此选项卡被杀死(但不是主窗口)。 在这种情况下,Close()方法对我不起作用。

您也可以尝试使用Chrome。 您必须提供URL作为实际程序的参数而不是URL本身,否则proc为null。

以下是使用默认浏览器的完整示例:

  string browser = string.Empty; RegistryKey key = null; try { key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command"); //trim off quotes if (key != null) { browser = key.GetValue(null).ToString().ToLower().Trim(new[] { '"' }); } if (!browser.EndsWith("exe")) { //get rid of everything after the ".exe" browser = browser.Substring(0, browser.LastIndexOf(".exe", StringComparison.InvariantCultureIgnoreCase) + 4); } } finally { if (key != null) { key.Close(); } } Process proc = Process.Start(browser, "http://www.google.nl"); if (proc != null) { proc.Kill(); }