2

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!

Armin
  • 81
  • 3
  • 9

2 Answers2

4

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).

varocarbas
  • 12,354
  • 4
  • 26
  • 37
0

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.

Falgantil
  • 1,290
  • 12
  • 27
  • Ah, right, you want it to open in a new window. I'm not entirely sure how that would be done, however I know that many people (including myself) despise having a program that opens a website in a new window, instead of the current browser. But your choice. – Falgantil Oct 14 '13 at 17:57