7

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?

Pandemonium1x
  • 81
  • 1
  • 1
  • 5

2 Answers2

11

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
} 
emartel
  • 7,712
  • 1
  • 30
  • 58
  • @emartel - I'm getting this error `Object reference not set to an instance of an object.`at this line - `foreach (string Keyname in OurKey.GetSubKeyNames())`. Any idea why I'm getting this? – SK. Nov 05 '14 at 06:34
  • @Mike possibly because `OpenSubKey` failed? – emartel Nov 06 '14 at 03:46
  • @emartel ya it coming as null... any idea why it is coming as null? I don't have admin rights may be this is the issue. – SK. Nov 06 '14 at 06:28
  • @Mike You should probably give a look at http://stackoverflow.com/questions/13728491/opensubkey-returns-null-for-a-registry-key-that-i-can-see-in-regedit-exe – emartel Nov 06 '14 at 12:18
  • What if you don't know the name of the key? – cptalpdeniz Dec 22 '22 at 00:03
  • @cptalpdeniz I'm confused - what are you trying to do if you don't know the name of the key? – emartel Dec 29 '22 at 15:57
1

Windows NT

Please do not miss space

bhuang3
  • 3,493
  • 2
  • 17
  • 17