Question

I'm new to c# and seeking for help.

I am distributing software packages using the Windows GPO feature. Sometimes specific clients need to have a package re-installed, but unfortunately this cannot be controlled by the default GPO editor. 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:

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. 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?

Thankful for any input!

Was it helpful?

Solution

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:

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

    Registry.LocalMachine.DeleteSubKey(subKey + "\\" + uniqueId);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top