我正在尝试开发一种方法来读取机器上安装的程序。
public void refreshProgramsFromWindows () {
string SoftwareKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products";
RegistryKey rk = default(RegistryKey);
rk = Registry.LocalMachine.OpenSubKey(SoftwareKey);
//string skname = null;
string sname = string.Empty;
// New list from scratch
this.installedSoftwareList = new List<software>();
// Object software info
software aSoftware = new software();
foreach (string skname in rk.GetSubKeyNames())
{
// Reset software info
aSoftware.reset();
try
{
// Name of the programm
sname = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skname).OpenSubKey("InstallProperties").GetValue("DisplayName").ToString();
aSoftware.name = sname;
// Write program to the list
installedSoftwareList.Add(aSoftware);
}
catch (Exception ex)
{
}
}
Net Framework 是 4.5,我超过了 Windows 7/8。当我调试这段代码时,var rk 为空,它在 foreach 中抛出了一个空引用异常。应用清单设置为需要管理员权限,因此注册表是可读的。问题是什么?
提前谢谢你。干杯。