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

后端 未结 4 496
渐次进展
渐次进展 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: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);
            }
        }
    

提交回复
热议问题