0

我是 c# 新手并寻求帮助。

我正在使用 Windows GPO 功能分发软件包。有时特定的客户端需要重新安装软件包,但不幸的是,这无法通过默认 GPO 编辑器进行控制。您可以启动软件包的重新部署,但它会再次在所有客户端上重新安装。

安装状态保存在客户端注册表中。每个软件包在此位置都有一个子键:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\AppMgmt

要在列表中收集远程计算机的子密钥,我执行以下操作:

List<string> RegFolders = new List<string>();
string subKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\AppMgmt";
RegistryKey AppMgmtKey;
RegistryKey AppKey;

AppMgmtKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, remoteHost).OpenSubKey(subKey);

foreach (string subKeyName in AppMgmtKey.GetSubKeyNames())
                {
                    RegFolders.Add(String.Join(",", subKeyName));
                }

我要将找到的值添加到选中的列表框中。由于子键被命名为唯一 ID,我将获取每个包含产品描述的子键的字符串值:

foreach (string DeplApp in RegFolders)
{
    AppKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, remoteHost).OpenSubKey(GlobalVars.subKey + "\\" + DeplApp);
    string DispName = (string)AppKey.GetValue("Deployment Name");
    cListBoxSubKeys.Items.Add(DispName);
}

到目前为止一切顺利,产品名称已列在列表框中。我遇到的问题是尝试删除已检查产品时的下一步:

foreach (object itemChecked in cListBoxSubKeys.CheckedItems)
{
    Registry.LocalMachine.DeleteSubKey(subKey + "\\" + itemChecked);
}

如果我将子键的唯一 ID 添加到选中列表框,这将有效。但是当我添加产品名称时,这当然不起作用。有没有一种简单的方法可以删除整个子键而不必在选中的列表框中列出 ID?

感谢您的任何意见!

4

1 回答 1

0

您可以将任何对象添加到选中的列表框,并指定要显示的类的哪个属性:

cListBoxSubKeys.Items.Add(new { Name = DispName, ID = UniqueId });
cListBoxSubKeys.DisplayMember = "Name";
cListBoxSubKeys.ValueMember = "ID";

然后在另一个事件中,将 id 读回:

foreach (object itemChecked in cListBoxSubKeys.CheckedItems)
{
    var uniqueId = itemChecked.GetType().GetProperty("ID").GetValue(itemChecked, null);

    Registry.LocalMachine.DeleteSubKey(subKey + "\\" + uniqueId);
}
于 2014-02-01T00:07:07.390 回答