2

I am writing a consumer application for TwitPic with OAuth Echo.

I need to navigate the user to a web page when they try to get a pin code from Twitter.

How can I navigate them from a WPF form when the consumer clicks a button?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tugberk
  • 57,477
  • 67
  • 243
  • 335
  • Are you using the WebBrowser control inside a WPF Window? – dugas Mar 30 '11 at 22:02
  • Do you just want to launch a browser, or do you want your app to host the web page? – John Myczek Mar 30 '11 at 22:08
  • do I wanna open the user's default web browser and go to a web page on button click. for instance, go to http://google.com – tugberk Mar 30 '11 at 22:22
  • Use an InternetExplorer object. It is the same as a WebBrowser except without the InternetExplorer object does not have a window. Add a reference for SHDocVw for it. – Sam Hobbs Apr 06 '15 at 03:34

3 Answers3

10

To launch the user's default browser to a specific web page you can do the following:

System.Diagnostics.Process.Start("http://www.google.com");
John Myczek
  • 12,076
  • 4
  • 31
  • 44
6

You could host a WebBrowser control in it's own window and open that window from your button click:

Window Hosting the BrowserControl:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <WebBrowser x:Name="Browser" ></WebBrowser>
</DockPanel>

In the click event of your button, use the WebBrowser control's Navigate method:

Window1 w = new Window1();
        w.Browser.Navigate(new Uri("http://stackoverflow.com"));
        w.Show();
dugas
  • 12,025
  • 3
  • 45
  • 51
  • thanks for the reply. as I said I am using this to get a pin code from twitter for TwitPic pic upload user application. do you think it will work. I will definitely give it a try though. – tugberk Mar 30 '11 at 22:24
  • yeah it worked. thanks. but how can I do it the other way, I am not sure yet :S – tugberk Mar 30 '11 at 23:48
1
Process.Start(new ProcessStartInfo("https://www.example.com") { UseShellExecute = true });

Note that I'm setting UseShellExecute = true

The default is true on .Net Framework, and false on .Net Core apps. You need to set UseShellExecute to true if you want to open a url using the default browser.

Michael Santos
  • 466
  • 7
  • 15