41

Is there a way I can launch a tab (not a new Window) in Google Chrome with a specific URL loaded into it from a custom app? My application is coded in C# (.NET 4 Full).

I'm performing some actions via SOAP from C# and once successfully completed, I want the user to be presented with the end results via the browser.

This whole setup is for our internal network and not for public consumption - hence, I can afford to target a specific browser only. I am targetting Chrome only, for various reasons.

Stephen Kennedy
  • 16,598
  • 21
  • 82
  • 98
miCRoSCoPiC_eaRthLinG
  • 2,780
  • 4
  • 35
  • 54

4 Answers4

56

As a simplification to chrfin's response, since Chrome should be on the run path if installed, you could just call:

Process.Start("chrome.exe", "http://www.YourUrl.com");

This seem to work as expected for me, opening a new tab if Chrome is already open.

Stephen Kennedy
  • 16,598
  • 21
  • 82
  • 98
Dylan Watson
  • 2,103
  • 1
  • 17
  • 38
  • Apologies, I've just used the wrong terminology but it does work. Just checked and it isn't on the PATH, it is definitely on the run path (whatever that actually uses) though - if you bring up the 'Run' prompt and run `chrome.exe "http:\\www.YourUrl.com"` it appears to behave the same way as C#. – Dylan Watson Mar 16 '12 at 03:10
  • 1
    Is there a way to know whether Chrome finished loading this URL (no longer busy), programatically, using C# as well? Something like [this](https://social.technet.microsoft.com/forums/scriptcenter/en-US/70e63239-7c97-4176-a060-b95676deb4fe/vb-script-to-detect-a-webpage-is-loaded-or-not), but not with VBA but with C#. – Hugo M. Zuleta Apr 28 '15 at 21:48
38
// open in default browser
Process.Start("http://www.stackoverflow.net");

// open in Internet Explorer
Process.Start("iexplore", @"http://www.stackoverflow.net/");

// open in Firefox
Process.Start("firefox", @"http://www.stackoverflow.net/");

// open in Google Chrome
Process.Start("chrome", @"http://www.stackoverflow.net/");
Hakan Fıstık
  • 11,376
  • 8
  • 74
  • 105
  • 5
    For me it did't worked with .NET Core 3.0 in winforms application. Instead I have to use `Process process = new Process();` `process.StartInfo.UseShellExecute = true;` `process.StartInfo.FileName = "chrome";` `process.StartInfo.Arguments = @"http://www.stackoverflow.net/";` `process.Start();` – Pavel Sapehin Feb 09 '19 at 16:01
  • 1
    Edge is a bit different also: https://stackoverflow.com/a/39626953/685404 – Josh Mc Oct 30 '20 at 03:45
23

UPDATE: Please see Dylan's or d.c's anwer for a little easier (and more stable) solution, which does not rely on Chrome beeing installed in LocalAppData!


Even if I agree with Daniel Hilgarth to open a new tab in chrome you just need to execute chrome.exe with your URL as the argument:

Process.Start(@"%AppData%\..\Local\Google\Chrome\Application\chrome.exe", 
              "http:\\www.YourUrl.com");
Christoph Fink
  • 21,159
  • 9
  • 62
  • 101
  • Question: Does this guarantee than an existing instance of Chrome will be used if one is already executing, and (if so) that the page will be opened in a new tab in lieu of a new window? – Mike Hofer Jun 10 '11 at 11:29
  • I can't guarantee it (may search the Chome settings for such a setting), but I tested it and it used the existing Chrome instance... – Christoph Fink Jun 10 '11 at 12:34
  • Please refer to the question to see why Chrome and why not other browsers. @Mike Hofer: Yep. It's pretty much guaranteed that Chrome is present on all the systems, especially for those users who use SugarCRM. @chrfin: Thanks! Let me give it a shot and get back to you. – miCRoSCoPiC_eaRthLinG Jun 10 '11 at 13:24
  • Please be aware that Google Chrome is not always installed in the path stated above. – Cray Oct 13 '14 at 08:44
  • 1
    You can also use '%localappdata%' instead of jumping up from Roaming for windows 7 and 8 (possibly newer) – Robert Snyder Feb 09 '15 at 20:35
  • 1
    I've got mine under `C:\Program Files (x86)\Google\Chrome\Application` – Matias Cicero Sep 29 '16 at 00:46
  • System.Diagnostics.Process.Start("chrome.exe", "http:\\www.YourUrl.com"); worked for me. – Elyas Nategh Nov 14 '20 at 12:48
6

If the user doesn't have Chrome, it will throw an exception like this:

    //chrome.exe http://xxx.xxx.xxx --incognito
    //chrome.exe http://xxx.xxx.xxx -incognito
    //chrome.exe --incognito http://xxx.xxx.xxx
    //chrome.exe -incognito http://xxx.xxx.xxx
    private static void Chrome(string link)
    {
        string url = "";

        if (!string.IsNullOrEmpty(link)) //if empty just run the browser
        {
            if (link.Contains('.')) //check if it's an url or a google search
            {
                url = link;
            }
            else
            {
                url = "https://www.google.com/search?q=" + link.Replace(" ", "+");
            }
        }

        try
        {
            Process.Start("chrome.exe", url + " --incognito");
        }
        catch (System.ComponentModel.Win32Exception e)
        {
            MessageBox.Show("Unable to find Google Chrome...",
                "chrome.exe not found!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
Stephen Kennedy
  • 16,598
  • 21
  • 82
  • 98
C0LD
  • 131
  • 1
  • 2