在C#下打开一个浏览器窗口作为弹出窗口

时间:2013-10-13 13:54:29

标签: c# .net browser process popup

我正在尝试使用C#将浏览器打开为pop。基本上,我正在检测用户何时启动浏览器,并且我想在此之后立即打开一个新的浏览器窗口,该窗口位于用户打开的窗口下。以下是我到目前为止的情况:

url = "example.com";
Process[] pname = Process.GetProcessesByName("chrome");

if (pname.Length == 0) // if the user didn't start chrome
{
    chrome = false;
}
else // if chrome is running
{
    if (!chrome) // if the browser wasn't opened before and it was opened just now.
    {
        Process process = new Process();
        process.StartInfo.FileName = "chrome";
        process.StartInfo.Arguments = url;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
        process.Start();
        //Process.Start("chrome", url);
    }

    chrome = true;
}

但这样做有时会在新窗口中打开,有时会在新标签中打开。也许我应该使用不同的方式启动浏览器或其他东西?谢谢!

2 个答案:

答案 0 :(得分:3)

当您拨打外部程序(在这种情况下为Chrome)时,您必须“遵守其规则”。通过查看Chrome参数(list here),--new-window可以提供您想要的内容。因此,您必须在代码中执行以下更改:

process.StartInfo.Arguments = url + " --new-window";

如果您想要完全控制,也许您应该创建自己的浏览器(例如WebBrowser Class)。

答案 1 :(得分:0)

我建议使用URL作为文件名。因为在执行此操作时,系统将自动检测路径是否为Web位置,并启动用户默认浏览器。 这也将确定浏览器是否已经打开,如果是,则在该窗口中打开而不是启动新的。

大多数人都喜欢这个,而不是启动Internet Explorer的程序,或者是你的Chrome浏览器。

您的问题有点模糊,如果您找到答案,请告诉我们。