如何使用C#启动包含特定网址的Google Chrome标签

miC*_*inG 33 c# url tabs google-chrome

有没有办法可以在Google Chrome中启动标签页(不是新窗口),并在自定义应用中加载特定的网址?我的应用程序使用C#(.NET 4 Full)编码.

我正在通过C#的SOAP执行一些操作,一旦成功完成,我希望通过浏览器向用户呈现最终结果.

整个设置适用于我们的内部网络,而不是公共消费 - 因此,我可以承受仅针对特定浏览器的费用.由于各种原因,我针对Chrome.

Dyl*_*son 43

作为chrfin响应的简化,由于Chrome应该在运行路径上安装,你可以调用:

Process.Start("chrome.exe", "http://www.YourUrl.com");
Run Code Online (Sandbox Code Playgroud)

这似乎对我有效,如果Chrome已经打开,则会打开一个新标签.

  • 好的,"运行......"确实有效,但是从命令promt开始并不... (4认同)

小智 31

// 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/");
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,它不适用于 winforms 应用程序中的 .NET Core 3.0。相反,我必须使用 `Process process = new Process();` `process.StartInfo.UseShellExecute = true;` `process.StartInfo.FileName = "chrome";` `process.StartInfo.Arguments = @"http:// www.stackoverflow.net/";``process.Start();` (5认同)

Chr*_*Fin 22

更新:请参阅Dylan或dc's anwer以获得更简单(更稳定)的解决方案,该解决方案不依赖于安装的Chrome设备LocalAppData!


即使我同意Daniel Hilgarth在chrome中打开一个新选项卡,您只需要使用您的URL作为参数执行chrome.exe:

Process.Start(@"%AppData%\..\Local\Google\Chrome\Application\chrome.exe", 
              "http:\\www.YourUrl.com");
Run Code Online (Sandbox Code Playgroud)

  • 我的目录位于“C:\Program Files (x86)\Google\Chrome\Application”下 (2认同)

小智 5

如果用户没有安装Chrome,则会抛出如下异常:

    //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);
        }
    }
Run Code Online (Sandbox Code Playgroud)