简体   繁体   English

如何在Windows上使用C#获取chrome.exe的路径?

[英]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.

相关问题 C# + .NET:在电脑中找到chrome.exe之类app的路径(所有磁盘,所有目录) - C# + .NET : Find the path of an app like chrome.exe in the computer (all disks, all directories) C#,如何在Windows Powershell(Windows 10)上配置Mono? (环境变量PATH) - C#, How Do I Get Mono Configured on Windows Powershell (Windows 10)? (Environmental Variable PATH) 如何获取C#控制台应用程序的.exe名称? - How do I get the .exe name of a C# console application? 我如何获取当前路径而不是 c# 中的 exe 路径? - How can iI get the current path and not the path of exe in c#? 在C#中使用.exe路径安装Windows服务 - installing a windows service with .exe path in c# C#如何使用Directory.GetFiles()获取与Windows资源管理器中的顺序相同的文件? - C# How do I use Directory.GetFiles() to get files that have the same order as in Windows explorer? 如何在 c# 控制台应用程序中获取 exe 的初始路径 - How to get initial path of exe in c# console application C#如何获取嵌入式.exe文件的PATH - C# How to get the PATH of the embedded .exe file C#如何获取系统程序集的路径 - C# How do I get the path to the System assembly 如何获取C#中注册的COM服务器的路径? - How do I get the path of a registered COM server in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM