views:

235

answers:

5

Hi,
I need to launch a browser, do some work and then make the browser navigate to a URL (in that order).
The first part is of course simple and I have a Process object. I am at a loss as to how to later direct it to the target page?

How do I treat the Process as a browser and make it navigate to the desired page? Any help, pointers, code snippets appreciated.

A: 

If you don't need the actual instance of IE, you can use the System.Windows.Forms.WebBrowser control.

eulerfx
Actually, I am using that for the desktop version of the application. For the web application, I need to use the default browser on the m/c.
Mohit Chakraborty
MohitC: If this is "for the web application" how are you even going to get a Process object on the end-user's machine? Is this in a controlled (intranet) environment? I'm clearly not understanding the scenario.
Stephen P
The app will run locally but needs to spawn a browser, so getting a Process object works the same as an app hosting a winform.
Mohit Chakraborty
A: 

I think instead of sending the browser a url you could send it javascript that would run and direct the browser to a site.

Not sure if this would work but I see no reason why it wouldn't

George Mauer
The question is about _how_ to later use the Process object, make it behave like the browser that it is. Sending a URL or JS is the same thing.
Mohit Chakraborty
Couldn't you pass it in as an argument when launching the process? I think I might be misunderstanding your requirements
George Mauer
That option is not a possibility here. As I said in the question-1. need to launch a browser2. do some work3. make the browser navigate to a URLin that order.
Mohit Chakraborty
ahhh...thats above my paygrade, sorry.
George Mauer
+1  A: 

My first instinct for this question was DDE, but it appears that has been decommissioned in Windows Vista so that is no good. Shame, as it was the only consistent mechanism in Windows for Interprocess Communication (IPC)...oh how I miss Arexx on the Amiga.

Anyhow, I believe the following will work but unfortunately, due to the way it works, it launches Internet Explorer irrespective of the configured browser.

  1. If your application has a Form, then create a WebBrowser control on it. Set this to non-visible as we are only making use of its as a launching device rather than to display the web page.
  2. In code, at the point where you want to show a web page, use the following code:

    webBrowser1.DocumentText = "window.open('http://stackoverflow.com/questions/715441/how-to-launch-a-browser-and-later-direct-it-to-a-page', 'BananasAreOhSoYummy');";

What this does is to tell the WebBrowser control, which is just the IE in disguise, to open a new window called 'BananasAreOhSoYummy'. Because we have given the window a name, we can use that line repeatedly, with different URLs, to change the page in that particular browser window. (A new window will be opened if the user has happened to close it.)

I will have a think about an approach that honours the user's default browser choice.

Paul Ruane
+2  A: 

Instead of launching the browser & then navigating to the page, just tell the OS that you want to run the URL. Windows will pick the correct browser, and navigate the user to the given URL.

System.Diagnostics.Process.Start("http://www.StackOverflow.com");
David
Pasting comment that I gave for another same answer - As I said in the question- 1. need to launch a browser 2. do some work 3. make the browser navigate to a URL _in that order_.
Mohit Chakraborty
+1  A: 

If you don't need to do this in production, you could use a testing library such as WatiN to do this:

using WatiN.Core;

 //Placeholder page to launch initial browser
 IE ie = new IE("http://www.google.com");

 DoSomeWork();

 //Now navigate to the page you want
 ie.GoTo("http://stackoverflow.com");
Stuart Caborn
I might use it for demo purpose, but it will surely get shot down by mgmt from being integrated into our commercial application. Thanks anyway.
Mohit Chakraborty
@MohitC - yes, I kind of assumed you wanted to use this for testing. This is not suitable for a production solution. I'll update my answer to reflect that.
Stuart Caborn