0

Using Visual Studio 2019 WPF app Trying to open a web page with code that has worked for me before but i get an error now

I get an error message saying System.ComponentModel.Win32Exception: 'The system cannot find the file specified.'

Also want to open the page in Chrome

using System;
using System.Windows;
using System.Diagnostics;

private void Button_Click_1(object sender, RoutedEventArgs e)
    {

        Process.Start("https://www.google.com");

    }
Bigshop
  • 15
  • 1
  • 3

2 Answers2

2

I guess you are using .net core. You should pass ProcessStartInfo to Process.Start(). If you want the default browser to open, you can call cmd to do the work for you. If you want a specific browser, you can provide the process name for that browser as well.

For default browser, you can do something like

string url = "https://www.google.com";
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
rabink
  • 679
  • 8
  • 13
  • That works Thanks -- How could i get the web page to open in Chrome – Bigshop Jul 12 '20 at 05:48
  • This is the code that actually works. There are some other responses where "Explorer" is called using a url as parameter, that approach fails sometimes when url contains querystring parameters. – jpmir Jun 04 '21 at 23:31
-1

To open a URL in Chrome, populate the ProcessStartInfo() class properties as in the following:

using System.Diagnostics;
using System.Windows;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string url = "https://www.google.com";

            ProcessStartInfo startInfo = new ProcessStartInfo();

            // To open url in Chrome, specify startInfo.File = "chrome.exe"
            // To open url in FireFox specify firefox.exe
            // To open url in Microsoft Edge specify msedge.exe
            // To open url in default browser specify explorer.exe

            startInfo.FileName = "chrome.exe";
            startInfo.Arguments = url;
            Process.Start(startInfo);
        }
    }
}
Jamal
  • 398
  • 4
  • 9
  • 1
    This relies on a particular browser to be installed on user machine (and being on the path too). Relying on such things is a bad practice, instead use whatever they have as the "default" browser and let it handle the url. – Alejandro Jul 12 '20 at 21:35
  • Jamal - I like your code and i did try to run it but when it gets to the Process.Start it gives me the original error that i was getting - I get an error message saying System.ComponentModel.Win32Exception: 'The system cannot find the file specified.' - perhaps im missing a reference but i dont know hoe to diagnose it, can you help?? Thanks – Bigshop Jul 13 '20 at 04:27
  • @Bigshop, could you create a fresh WPF app and see what happens. Maybe you are missing some references. I commented earlier to do that, but some character deleted my comments! – Jamal Jul 15 '20 at 02:40