private void button2_Click(object sender, EventArgs e) { try { string app = "chrome.exe"; RegistryKey regKey = Registry.LocalMachine; RegistryKey regSubKey = regKey.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionApp 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。