我需要按程序名称(添加或删除程序中显示的名称)检查程序的安装位置。什么是最好的方法,以便它适用于所有语言。
答案 0 :(得分:12)
在
处查看注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
只需遍历所有子项,然后查看值 DisplayName 和 InstallLocation 。在这里,您可以找到您想要的信息以及更多信息; - )
答案 1 :(得分:8)
要添加到Oliver的答案中,我已将此检查包含在静态方法中。
public static bool IsProgramInstalled(string programDisplayName) {
Console.WriteLine(string.Format("Checking install status of: {0}", programDisplayName));
foreach (var item in Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall").GetSubKeyNames()) {
object programName = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + item).GetValue("DisplayName");
Console.WriteLine(programName);
if (string.Equals(programName, programDisplayName)) {
Console.WriteLine("Install status: INSTALLED");
return true;
}
}
Console.WriteLine("Install status: NOT INSTALLED");
return false;
}
答案 2 :(得分:4)
答案 3 :(得分:0)
您可以使用WMI类来实现此目的。但先决条件是
在示例代码下方执行此操作
string queryString =
"SELECT Name, ProcessId, Caption, ExecutablePath" +
" FROM Win32_Process";
SelectQuery query = new SelectQuery(queryString);
ManagementScope scope = new System.Management.ManagementScope(@"\\.\root\CIMV2");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection processes = searcher.Get();
foreach(ManagementObject mObj in processes)
{
var name = mObj ["Name"].ToString();
var ProcessId = Convert.ToInt32(mObj ["ProcessId"]);
var Caption = mObj ["Caption"].ToString();
var Path = mObj ["ExecutablePath"].ToString();
}