Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Registry Keys in C#

Tags:

c#

registrykey

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?

like image 808
Pandemonium1x Avatar asked Nov 16 '12 13:11

Pandemonium1x


2 Answers

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
} 
like image 200
emartel Avatar answered Nov 01 '22 22:11

emartel


Windows NT

Please do not miss space

like image 24
bhuang3 Avatar answered Nov 01 '22 23:11

bhuang3