How to launch a Google Chrome Tab with specific URL using C#

后端 未结 4 480
渐次进展
渐次进展 2020-12-08 13:33

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

相关标签:
4条回答
  • 2020-12-08 13:40

    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");
    
    0 讨论(0)
  • 2020-12-08 13:41
    // 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/");
    
    0 讨论(0)
  • 2020-12-08 13:52

    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.

    0 讨论(0)
  • 2020-12-08 13:58

    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);
            }
        }
    
    0 讨论(0)
提交回复
热议问题