How to open a web browser from a Windows App?  
Author Message
Jimmy_fingers





PostPosted: Visual C# General, How to open a web browser from a Windows App? Top

Could someone please tell me how to open a web browser by pressing a button on a Windows App that goes to a specific website

Many thanks


Visual C#11  
 
 
ahmedilyas





PostPosted: Visual C# General, How to open a web browser from a Windows App? Top

launch it with the Process class:

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

this will launch it in the default webbrowser of the user.

you can also customize the way the process should be started by using the ProcessStartInfo class:

http://msdn2.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

 



 
 
Peter Ritchie





PostPosted: Visual C# General, How to open a web browser from a Windows App? Top

One thing to keep in mind is System.Diagnostics.Process.Start("http://url") can only be performed on the main thread. Starting another application, especially the web browser is notoriously slow (at least a second or two). While that's occurring your applications message loop will be stalled and cannot process vital messages (like paint message) and if stalled long enough XP and Vista may add "Not responding" to the window's caption (aka ghost window).

I recommend starting external browser applications from a background thread, which requires jumping through a few hoops:

private System.ComponentModel.BackgroundWorker spawnBrowserBackgroundWorker;

void Initialize ( )

{

this.spawnBrowserBackgroundWorker = new System.ComponentModel.BackgroundWorker();

this.spawnBrowserBackgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.spawnBrowserBackgroundWorker_DoWork);

}

/// <summary>

/// BackgroundWorker.DoWork event handler.

/// Just spawn a process to handle the URL based on the system's file association.

/// </summary>

private void spawnBrowserBackgroundWorker_DoWork ( object sender, DoWorkEventArgs e )

{

String url = e.Argument as String;

if (url != null)

{

Process process = new Process();

process.StartInfo.FileName = "rundll32.exe";

process.StartInfo.Arguments = "url.dll,FileProtocolHandler " + url;

process.StartInfo.UseShellExecute = true;

process.Start();

}

}

public void SpawnURL ( string url )

{

//...

// Asynchronously spawn a browser as this may take

// several seconds.

// Use BackgroundWorker to avoid queueing with the thread pool

// and avoid delay with Control.BeginInvoke.

spawnBrowserBackgroundWorker.RunWorkerAsync(url);

}



 
 
Ken_L





PostPosted: Visual C# General, How to open a web browser from a Windows App? Top

Peter & Ahmediyas,

Just out of curiosity, why didn't you direct him to opening a brower control in a new form It seems like opening a new IE makes a solution generic where a form that has the application icon and possibly no browser controls customizes the solution...

Obviously, the apps business rules dictate...



 
 
ahmedilyas





PostPosted: Visual C# General, How to open a web browser from a Windows App? Top

Ken: I believe the user was specific on what they wanted therefore the appropriate response was given :-) Otherwise should it be a "blurry" type question, all possible solutions would be given.

 
 
Peter Ritchie





PostPosted: Visual C# General, How to open a web browser from a Windows App? Top

Ken, there's all sorts of reasons why you may want to simply open IE (or the default browser) for a URL. For example, you may want to maintain a global history, or want to use global cookies, you want to use an IE toolbar, or you may be deploying to an environment where most applications are blocked from accessing the Internet.

 
 
Peter Ritchie





PostPosted: Visual C# General, How to open a web browser from a Windows App? Top

Ken, there's all sorts of reasons why you may want to simply open IE (or the default browser) for a URL. For example, you may want to maintain a global history, or want to use global cookies, you want to use an IE toolbar, or you may be deploying to an environment where most applications are blocked from accessing the Internet.
Actually, I'm wrong with the global history, it turns out if you visit a site in a WebBrowser control that site appears in the history list in IE... But, cookies are not shared between IE and a WebBrowser control. I haven't verified, but I would *really* hope they're not shared between WebBrowser control instances.

 
 
Ken_L





PostPosted: Visual C# General, How to open a web browser from a Windows App? Top

Thank you Gentlemen!!!!