[英]How do I use C# to get the path to chrome.exe on Windows?
I want to launch chrome from my automated test framework so that I can test my server-side ASP.NET code. 我想从自动化测试框架中启动chrome,以便可以测试服务器端ASP.NET代码。 What's the best way to determine the location of where chrome.exe is located on my computer?
确定chrome.exe在我的计算机上的位置的最佳方法是什么?
When Chrome is installed on a computer, it installs the ChromeHTML
URL protocol. 在计算机上安装Chrome后,它将安装
ChromeHTML
URL协议。 You could use that to get to the path for Chrome.exe. 您可以使用它来访问Chrome.exe的路径。
Some example code may help. 一些示例代码可能会有所帮助。 The following code returns a string that looks like this:
以下代码返回一个看起来像这样的字符串:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -- "%1"
Example code to get that: 可以得到的示例代码:
var path = Microsoft.Win32.Registry.GetValue(
@"HKEY_CLASSES_ROOT\ChromeHTML\shell\open\command", null, null) as string;
if (path != null)
{
var split = path.Split('\"');
path = split.Length >= 2 ? split[1] : null;
}
if path is null at the end of the code snippet, then you can assume Chrome isn't installed. 如果在代码段的末尾path为null,则可以假定未安装Chrome。
Another approach is use the logic used by the Karma test framework. 另一种方法是使用Karma测试框架使用的逻辑。
const string suffix = @"Google\Chrome\Application\chrome.exe";
var prefixes = new List<string> {Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)};
var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
var programFilesx86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
if (programFilesx86 != programFiles)
{
prefixes.Add(programFiles);
}
else
{
var programFilesDirFromReg = Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion", "ProgramW6432Dir", null) as string;
if (programFilesDirFromReg != null) prefixes.Add(programFilesDirFromReg);
}
prefixes.Add(programFilesx86);
var path = prefixes.Distinct().Select(prefix => Path.Combine(prefix, suffix)).FirstOrDefault(File.Exists);
if path is null at the end of the code snippet, then you can assume Chrome isn't installed. 如果在代码段的末尾path为null,则可以假定未安装Chrome。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.