-3

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#

Soulss
  • 171
  • 1
  • 12
  • See here: Have you a browser installed and a default browser is set ? https://learn.microsoft.com/en-us/troubleshoot/dotnet/csharp/start-internet-browser – Nawed Nabi Zada Dec 20 '21 at 07:11
  • @NawedNabiZada Yes, I have Google Chrome as my default browser. But regardless of that, even if the user doesn't have a default browser set, is there a simple way of opening a website from the WPF? – Soulss Dec 20 '21 at 07:14
  • This is the simple way. Please read the whole article, it explains why you get that exception – Nawed Nabi Zada Dec 20 '21 at 07:15
  • @NawedNabiZada Using the try-catch from this article it says i don't have a browser, but I do. So the only thing I believe it can be is something related to this HKEY_CLASSES_ROOT. But even so, this doesn't seems to be a reliable way of opening a website from the WPF, cause even with me having a browser and set to the defaul, it doesn't work... – Soulss Dec 20 '21 at 07:23
  • 1
    If you type in `https://website.com/` in the Run dialog (Windows+R) or a cmd window: does it work? – Klaus Gütter Dec 20 '21 at 08:08
  • If there is no default browser, which application do you want to use for navigating to the URL...? – mm8 Dec 20 '21 at 14:11
  • @KlausGütter If i type ```explorer "https://www.website.com/"``` in the cmd, it works perfeclty, that's exactly what I wish i could do when the button was clicked. It opens the website with my default browser which is Chrome – Soulss Dec 20 '21 at 19:11

2 Answers2

0

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 });
}
Soulss
  • 171
  • 1
  • 12
-1

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);
}
MD. RAKIB HASAN
  • 3,670
  • 4
  • 22
  • 35