How to open a link in the same browser window or tab from Process.Start?

I am trying to open a browser window from LinkLabel in a windows form. When the button is clicked, control passes to the LinkClicked event, and the code calls the default browser using:

System.Diagnostics.Process.Start("http://www.google.com"); 

I would like to be able to click the link (i.e. start Start several times), but only in the same browser window or tab. Of course, a few clicks each time opens a new tab in Google. I know how to specify a named window using the link:

 <a href="http://www.google.com" target="googlewin">Click Here!</a> 

But how would this be done in the Start team?

ETA: I clicked a link to a link related to Internet Explorer in the "About" form, and each time it opens a new window, so maybe even Microsoft cannot do this. Hmmm.

+6
source share
3 answers

For Internet Explorer, you can do this using the SHDocVw assembly.

Instead of using process.start, just create an instance of SHDocVw.InternetExplorer and use it to navigate in the same browser whenever you want. Here is a simple example.

 private SHDocVw.InternetExplorer IE; private void Form1_Load(object sender, EventArgs e) { IE = new SHDocVw.InternetExplorer(); IE.Navigate("http://stackoverflow.com/"); IE.Visible = true; } private void button1_Click(object sender, EventArgs e) { IE.Navigate("http://google.com/"); } 

If you specifically want to use Process.start, for Internet Explorer you can iterate through SHDocVw.ShellWindows to find the Internet Explorer that you want to use for navigation.

 foreach (SHDocVw.InternetExplorer IE in new SHDocVw.ShellWindows()) { if (IE.FullName.ToLower.Contains("iexplore") & IE.LocationURL.ToLower.Contains("someurl")) { IE.Navigate("http://google.com/"); } } 
+1
source

Ditch Process.Start : This is most likely impossible. The contractor will need to support command line options that will allow you to select a tab and navigate. I do not see such support in any browser: Chrome ; IE or Firefox

The only reasonable alternative that I can think of is to use published interaction mechanisms to work with the browser window. When you have a browser window, you can script it to set the location of the window. With this approach, you will need to use a WebBrowser control or something like Awesomium .

0
source

I feel that you are approaching this wrong.

When clicked, the control goes to the LinkClicked event, and the code calls the default browser using:

In fact, it does not call the default browser, but creates a new default browser instance. Yes, you could get the handle of a newly created process and use a rather complicated way to add additional tabs, but this practice will reinvent the wheel. It is best to create your own WebBrowser control. Process.Start is a way to create a new process and should not be used as a means of modifying an already running process.

0
source

Source: https://habr.com/ru/post/980330/


All Articles