简体   繁体   English

列出和删除C#中的注册表子项

[英]List and delete registry subkey in C#

I'm new to c# and seeking for help. 我是C#的新手,正在寻求帮助。

I am distributing software packages using the Windows GPO feature. 我正在使用Windows GPO功能分发软件包。 Sometimes specific clients need to have a package re-installed, but unfortunately this cannot be controlled by the default GPO editor. 有时,特定的客户端需要重新安装软件包,但是不幸的是,这不能由默认的GPO编辑器控制。 You can initiate a redeploy of the package, but it will then re-install on all clients again. 您可以重新部署该程序包,但是它将再次在所有客户端上重新安装。

The installation status is saved in the clients registry. 安装状态保存在客户端注册表中。 Each software package has a sub key in this location: 每个软件包在此位置都有一个子键:

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

To collect the sub keys of a remote computer in a list I do the following: 要在列表中收集远程计算机的子键,请执行以下操作:

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));
                }

I am going to add the found values to a checked-listbox. 我将找到的值添加到选中列表框。 As the sub keys are named as unique IDs, I'm going to get a string value of each sub key which contains the product description: 由于子键被命名为唯一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);
}

So far so good, the product names are listed in the listbox. 到目前为止,产品名称已在列表框中列出。 The problem I have is with the next, step when trying to delete the checked products: 尝试删除已检查的产品时,下一步是我的问题:

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

This works if I would add the unique ID of the subkey to the checked-listbox. 如果我将子项的唯一ID添加到选中列表框中,则可以使用此方法。 But as I'm adding the product name, this of course does not work. 但是当我添加产品名称时,这当然不起作用。 Is there an easy way to delete the entire sub key without having to list the ID in the checked-listbox? 有没有一种简单的方法可以删除整个子键,而不必在检查列表框中列出ID?

Thankful for any input! 感谢您的输入!

You can add any object to the checked list box, and specify which property of the class to display: 您可以将任何对象添加到选中的列表框中,并指定要显示的类的属性:

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

Then in the other event, read the id back out: 然后在另一种情况下,读回ID:

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

    Registry.LocalMachine.DeleteSubKey(subKey + "\\" + uniqueId);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM