Finding Registry Keys in C#

Pandemonium1x picture Pandemonium1x · Nov 16, 2012 · Viewed 16k times · Source

I am working on a project that will allow me to delete the registry key from a Windows 7 PC. Specifically I am trying to make a program that will allow me to delete a profile from the machine via the ProfileList key. My problem is no matter what I try I can't seem to read the key correctly which I want to do before I start randomly deleting stuff. My code is

     RegistryKey OurKey = Registry.LocalMachine;
            OurKey = OurKey.OpenSubKey(@"SOFTWARE\Microsoft\WindowsNT\CurrentVersion\ProfileList", true);

            foreach (string Keyname in OurKey.GetSubKeyNames())
            {
                MessageBox.Show(Keyname);
            } 

This code runs but doesn't return anything (No MessageBox). Any ideas why not?

EDIT:

I got the top level keys to load thanks to you all but it does only show the folder/key names (Ex: S-1-5-21-3794573037-2687555854-1483818651-11661) what I need is to read the keys under that folder to see what the ProfilePath is. Would there be a better way to go about that?

Answer

emartel picture emartel · Nov 16, 2012

As pointed out by Lloyd, your path should use "Windows NT". In case of doubt, always use regedit to go inspect the registry manually.

Edit: To go with your edit, you can simply GetValue on the keys you find, the following code should do what you're looking for:

RegistryKey OurKey = Registry.LocalMachine;
OurKey = OurKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", true);

foreach (string Keyname in OurKey.GetSubKeyNames())
{
    RegistryKey key = OurKey.OpenSubKey(Keyname);

    MessageBox.Show(key.GetValue("KEY_NAME").ToString()); // Replace KEY_NAME with what you're looking for
}