Open a browser window as a pop under in C#

I'm trying to make a browser open as a pop under by using C#. Basically, I'm detecting when a browser is started by the user and I want to immediately after that open a new browser window which is under the window that the user opened. Here is what I have so far:

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

But what this does is sometimes open it in a new window and sometimes in a new tab. Perhaps I should use a different way of starting a browser or something? Thanks!

Answer1:

When you call an external program (Chrome in this case) you have to "follow its rules". By looking at the Chrome arguments (list here), --new-window delivers what you want. Thus you have to perform the following change in your code:

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

If you want to have full control, perhaps you should create your own browser (WebBrowser Class, for example).

Answer2:

What I would suggest is using the URL as the filename. Because when doing this, the system will automatically detect that the path is a web location, and start the users default browser. This will also figure out if a browser is already open, and if so, open in that window instead of launching a new one.

Most people prefer this, over the program launching Internet Explorer, or in your case, Chrome.

Your question is a bit vague, so please let us know if this is the answer you seek.

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Open a browser window as a pop under in C#