Home > Enterprise >  How to redirect to a Website from a Button click in a desktop app WPF?
How to redirect to a Website from a Button click in a desktop app WPF?

Time:12-21

I've seen everyone recomending the following:

        private void RegisterBtn(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://website.com/");
        }

But I get the following error:

"The system cannot find the file specified." System.ComponentModel.Win32Exception"

I want for the button to open a window in the default browser.

Ps: It's a WPF desktop App coded in C#

CodePudding user response:

So, i've found this answer: How to open a web page from WPF?

And that does exactly what I asked, Idk how to link that answer to this one. But here's the code:

private void RegisterBtn(object sender, RoutedEventArgs e)
{
    string url = "https://www.website.com";
    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
}

CodePudding user response:

Try using explorer.exe explicitly:
System.Diagnostics.Process.Start("explorer.exe", url);
or you can use it in try catch block:

try
{
  System.Diagnostics.Process.Start(url);
}
catch (Win32Exception ex)
{
  System.Diagnostics.Process.Start("IExplore.exe", url);
}
  • Related