如何获取chrome.exe路径(c#)

启动本机chrome浏览器可以直接使用

System.Diagnostics.Process.Start("chrome.exe");

但是当程序以管理员权限启动后(如何以管理员权限启动参考c#程序以管理员权限运行),上述代码可能会失效(测试环境win10+vs2019,其他环境未测试),提示“系统找不到指定文件”,这里就需要指定chrome.exe的全路径,现在提供两种方法获取chrome.exe的路径

1、通过注册表HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp Paths(这个不是一定能找到,本机测试时该路径下没有chrome.exe的信息)

绝大多数软件,基本上都会在注册表中记录自己的名字和安装路径信息。

在注册表中记录这些信息的位置是:

HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp Paths

因此,我们只要能访问到注册表的这个位置,就可以获取到某些软件的名称和安装路径信息。

 1 public string GetChromePath1(string exeName)
 2 {
 3   try
 4   {
 5     string app = exeName;
 6     RegistryKey regKey = Registry.LocalMachine;
 7     RegistryKey regSubKey = regKey.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionApp Paths" + app, false);
 8     object objResult = regSubKey.GetValue(string.Empty);
 9     RegistryValueKind regValueKind = regSubKey.GetValueKind(string.Empty);
10     if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
11     {
12       return objResult.ToString();
13     }
14     return "";
15   }
16   catch
17   {
18     return "";
19   }
20 }

2、通过ChromeHTML协议寻找

当Chrome安装在计算机上时,它会安装ChromeHTML URL协议,可以使用它来到Chrome.exe的路径。

ChromeHTML URL协议注册表路径为:

@"HKEY_CLASSES_ROOT" + "ChromeHTML" + @"shellopencommand"

需要注意的是上述字符串中间的"ChromeHTML"在有的电脑上是"ChromeHTML.QUDJD3GQ5B7NKKR7447KSMQBRY",所以采用以下代码获取:

 1 public string GetChromePath()
 2         {
 3             RegistryKey regKey = Registry.ClassesRoot;
 4             string path = "";
 5             string chromeKey = "";
 6             foreach (var chrome in regKey.GetSubKeyNames())
 7             {
 8                 if (chrome.ToUpper().Contains("CHROMEHTML"))
 9                 {
10                     chromeKey = chrome;
11                 }
12             }
13             if (!string.IsNullOrEmpty(chromeKey))
14             {
15                 path = Registry.GetValue(@"HKEY_CLASSES_ROOT" + chromeKey + @"shellopencommand", null, null) as string;
16                 if (path != null)
17                 {
18                     var split = path.Split('"');
19                     path = split.Length >= 2 ? split[1] : null;
20                 }
21             }
22             return path;
23         }
原文地址:https://www.cnblogs.com/MarcLiu/p/14096987.html