2013-10-13 237 views
2

我想通过使用C#使浏览器打开为弹出窗口。基本上,我正在检测用户何时启动浏览器,然后我想立即打开用户打开的窗口下的新浏览器窗口。以下是我迄今为止:在C#中打开浏览器窗口作为弹出窗口#

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; 
} 

但是,这并在一个新的窗口,有时在一个新的标签,有时打开它。也许我应该使用不同的方式来启动浏览器或其他东西?谢谢!

回答

3

当你调用外部程序(在这种情况下为Chrome)时,你必须“遵循其规则”。通过查看Chrome参数(list here),--new-window可提供您想要的内容。因此,你必须在你的代码执行下列变化:

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

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

+0

谢谢你!它的作品:) – armin

+0

@ArminAšimović不客气。 – varocarbas

0

我会建议使用URL作为文件名。因为执行此操作时,系统会自动检测路径是否为Web位置,并启动用户默认浏览器。 这也可以看出浏览器是否已经打开,如果是,打开窗口而不是启动一个新窗口。

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

你的问题有点含糊,所以请让我们知道这是你寻求的答案。

+1

我试过这个,它仍然在新选项卡中打开。 – armin

+0

啊,对,你想让它在新窗口中打开。我不完全确定如何做,但我知道很多人(包括我自己)都不喜欢有一个程序在新窗口中打开网站,而不是当前的浏览器。但你的选择。 – Falgantil

相关问题