18651

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

  • ProcessStartInfo Verb runas not working
  • Waiting For Process To Complete
  • Waitforexit interrupt my running application in unity
  • ASP.NET Process.Start not working on IIS8 (Windows 8.1)
  • C# cmd prompt cannot see telnet.exe
  • How can I add a WPF overlay over an external Win32 application's window?
  • Executing command in command prompt with redirection on a C# .aspx page
  • matlab gui radio buttons
  • In python how do I set the value of a LP_c_ubyte
  • Non-modal questdlg.m prompt
  • Prevent WPF Window From Minimizing (Winkey + D, mostly)
  • One-dimensional binpacking algorithm with relative costs
  • Closest value different files, with different number of lines and other conditions ( bash awk other)
  • setElementClass for multiple classes in angular2
  • PhoneGap build + jquerymobile: onclick on button does not work
  • Is it possible to set an Android Notification or a later date and time that will fire when the app i
  • ShellEx: Starting Excel minimized or hidden
  • Least square optimization in R
  • Does the MySQL IN clause execute the subquery multiple times?
  • How dotnet build chooses the output name
  • hide missing dates from x-axis ggplot2
  • Center align outputs in ipython notebook
  • List images(01.png) and descriptions(01.txt) from directory
  • Object and struct member access and address offset calculation
  • how to upload multiple files in c# windows application
  • How can I sort a a table with VBA with given text condition?
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Handling un-mapped Rest path
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Illegal mix of collations for operation for date/time comparison
  • MySQL WHERE-condition in procedure ignored
  • ActionScript 2 vs ActionScript 3 performance
  • How can I estimate amount of memory left with calling System.gc()?
  • Apache 2.4 - remove | delete | uninstall
  • 0x202A in filename: Why?
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • reshape alternating columns in less time and using less memory
  • How do I use LINQ to get all the Items that have a particular SubItem?