Open a Chrome Tab and Close it

c#, winforms

Solution

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();
        }

Problem

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.

Original source

Related problems