3

I want to open a Google Chrome tab, or default Browser. Then close it, after the user chooses to do so.

I am using

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

To open the Browser, but I don't have a handle on it to close it. As well I don't want to close the whole browser, just the tab I opened.

General Grey
  • 3,598
  • 2
  • 25
  • 32
  • Umm, doesn't the Process.Start command give you some sort of handle? It's of type Process, you can probably do something with it! I don't think you can close just the tab though...maybe if every tab has it's own process? – Davio May 22 '12 at 12:01
  • I tried Process ChromeTab = Process.Start(...) Then ChromeTab.Close() And I got an exception Object reference not set to an instance of an object. – General Grey May 22 '12 at 12:06

1 Answers1

5

This works for me in Firefox:

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

Because I have Firefox set to one-window mode it opens a tab. This tab is killed (but not the main window) when I issue the Kill() method. The Close() method didn't work for me in this case.

You could try the same with Chrome. You have to provide the URL as an argument to a real program instead of the URL itself, otherwise the proc is null.

Here's a full example using the default browser:

        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();
        }
Davio
  • 4,609
  • 2
  • 31
  • 58
  • I am in the process of trying this, is there a way to specify run default browser? – General Grey May 22 '12 at 12:12
  • No clue, maybe you have to dig in the registry... See http://uk.answers.yahoo.com/question/index?qid=20080105121336AAnheHM – Davio May 22 '12 at 12:14
  • If I run this in the same Method, it seems to work, Tab never opens (I assume it is opening and closing quickly) But if I move the kill to another method, it says Cannot process request because the process (6004) has exited. – General Grey May 22 '12 at 12:24
  • Well I gave you a +1 for effort alone so far, I will test this and let you know – General Grey May 22 '12 at 12:24
  • Well, I can't nor won't do everything myself, you have to put in some effort of your own. :) – Davio May 22 '12 at 12:28
  • Still having the same problem, it insists the Process has exited already. – General Grey May 22 '12 at 12:29
  • It works if Chrome(default) is not already open. But if it is open, it tells me it has exited, I think I will look into opening a window instead of a tab. – General Grey May 22 '12 at 12:33