使用C#检测电脑上是否安装某软件

时间:2023-03-09 19:15:26
使用C#检测电脑上是否安装某软件
 private void button2_Click(object sender, EventArgs e)
{
try
{
string app = "chrome.exe";
RegistryKey regKey = Registry.LocalMachine;
RegistryKey regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + app, false);
string strKey = string.Empty;
object objResult = regSubKey.GetValue(strKey);
RegistryValueKind regValueKind = regSubKey.GetValueKind(strKey);
if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
{
this.tb_path.Text = objResult.ToString();
}
}
catch
{
this.tb_path.Text = "未获取到路径,可能是没有安装chrome浏览器!";
}
}

如果电脑上安装了chrome,那么就会把路径显示在文本框中,如果没有安装,那么代码运行到   object objResult = regSubKey.GetValue(strKey);  就会报错,进入catch。