1

WinForms App / RSS News Feed Control

What I want to achieve is:

If user wants to open an URL from RSS news feed of the winApp, then

  • check if a default Browser is open
  • if no, then send to the Shell the URL - it's clear how to do.
  • if yes, then send this URL to the Browser's new tab and activate Browser and that Tab.

So, generally 1-st and 3-rd are the points of my interest to ask you.

caesay
  • 16,932
  • 15
  • 95
  • 160
  • What have you tried? Have you done any research or searching on points 1 and 3? what are you stuck on? – caesay Nov 23 '17 at 02:51
  • I know how to get the default browser name, but then I have to go through the list of processes to find if it's open, but I don't know the exact process name, if it's not one of widely using browsers. Well. if I would resolve that task, then how to send to that active process the command to open the URL not in new window, but in new tab. –  Nov 23 '17 at 03:04
  • You can just send the URL to the shell.. execute `explorer.exe "http://google.com"` to see this in action. If your default browser is open, it should automatically open a new tab. – caesay Nov 23 '17 at 03:05

3 Answers3

2
System.Diagnostics.Process.Start(startUrl); 

thats all. It checks if default browser is open, run it if not and open the url in new tab and activates it.

Antonín Lejsek
  • 6,003
  • 2
  • 16
  • 18
  • YEAH, GOOD. I didn't expect that WinShell will do it for me. Last time I was doing the same in WinXP, and it was unclear how it will work either will open new window or just new tab. Now I see Chrome under W7 does what I need by default. Cool. Thanks –  Nov 23 '17 at 03:46
1

is there a reason you need to do the first 2 parts? Why do you care what is the default browser? Will you change what you do?

What what are you sending the URL to shell for?

If using .NET, and when using Process class - when passing it a link, it will use the default browser. So the same application may launch Chrome or IE respectively.

There is another stack that shows how to do this already: Open a URL from Windows Forms

ProcessStartInfo sInfo = new ProcessStartInfo("http://example.com/");  
Process.Start(sInfo);
Poat
  • 327
  • 1
  • 11
  • YEAH, GOOD. I didn't expect that WinShell will do it for me. Last time I was doing the same in WinXP, and it was unclear how it will work either will open new window or just new tab. Now I see Chrome under W7 does what I need by default. Cool. Thanks. –  Nov 23 '17 at 03:42
0

This is obviously going to vary by browser, but its clear you've done almost no research on this so I'll be your research assistance.

Note

Generally you can just execute the URL on the path, and the default browser will open a new tab or start as nessesary. there shouldn't be any need to do the following. execute explorer.exe "http://google.com" in your CMD window to see how this works with various default browsers.

Answer

I googled "How to detect the default browser C#" which yielded several results, the first of which is How to find default web browser using C#? - this has a couple answers, the second one seems to be the one that you want.

Once you know the default browser, you need to check if it's open. So you will need to compile a list of binary names for each browser (chrome is chrome.exe) and we search for "check if process is running C#" and that gives us How can I know if a process is running?

Now, we know if the browser is running. If it is we will have a Process instance - so we need to now know the binary location (this is important!). So we search for Get the path of a running process C# and that gives us C#: How to get the full path of running process?

Each browser will have it's own mechanism for opening a new tab and receiving focus, but generally you need to execute the binary with some arguments. For Google Chrome, the answer is here: https://superuser.com/questions/731467/command-line-option-to-open-chrome-in-new-window-and-move-focus

So now we have completed everything for the google chrome browser. Repeat for other browsers, and do similar research to find out how to execute the binary to open a new tab.

If you want a generic solution that works for all browsers, this already exists. just execute explorer "yourUrl".

caesay
  • 16,932
  • 15
  • 95
  • 160
  • YEAH, GOOD. I didn't expect that WinShell will do it for me. Last time I was doing the same in WinXP, and it was unclear how it will work either will open new window or just new tab. Now I see Chrome under W7 does what I need by default. Cool. Thanks. –  Nov 23 '17 at 03:43