1

How Do I open google chrome using c#?

It shows System.ComponentModel.Win32Exception: 'The system cannot find the file specified'

I'd triedProcess.Start("C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe"); too, but it shows the same exception

using System;

using System.Diagnostics;

namespace tempTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Process.Start("chrome.exe");
        }
    }
}
  • you can find your answer here: https://stackoverflow.com/questions/2369119/error-in-process-start-the-system-cannot-find-the-file-specified – MarioWu Nov 19 '19 at 08:02
  • 6
    Never use hardcoded path. Consider using [`Environment.GetFolderPath()`](https://learn.microsoft.com/en-us/dotnet/api/system.environment.getfolderpath?view=netframework-4.8) along with [`Environment.SpecialFolder`](https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=netframework-4.8) **there is a space between File and (x86)** – Cid Nov 19 '19 at 08:04
  • It has to be chrome explicitly? Not the Default-Browser? – Fildor Nov 19 '19 at 08:14

1 Answers1

2

The chrome application path can be read from the registry. You can try following codes:

            var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe", false);

            if(key != null)
            {
                var path = Path.Combine(key.GetValue("Path").ToString(), "chrome.exe");

                if(File.Exists(path))
                {
                    Process.Start(path);
                }
            }
xiangbin.pang
  • 319
  • 1
  • 7
  • Chrome isn't necessary in HKLM. It can be in HKCU, since it **can** be installed for a single user instead of all users – Cid Nov 19 '19 at 13:29
  • @Cid, you are right, but I think we should try read both two base key locations to get the correct chrome installation path. – xiangbin.pang Nov 20 '19 at 00:52
  • Yes, of course. Hopefully, this is in the same key : `HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe` – Cid Nov 20 '19 at 08:22