Finding Registry Keys in C#
Asked Answered
C

2

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?

Crabbing answered 16/11, 2012 at 13:50 Comment(5)
What do you mean by "doesn't return anything"? Is there no MessageBox at all, or does one show up, but it's empty?Mylan
It's "Windows NT" and NOT "WindowsNT".Cakewalk
Ha Genius. I must be getting old. That worked thanks!Crabbing
It would be a lot better if you worked towards P/Invoking DeleteProfile (i.e. the official API for removing a profile) rather than spelunking through the registry.Telling
Interesting I did not know there was AN API for that. You may have changed this whole project for me.Crabbing
Q
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
} 
Quartus answered 16/11, 2012 at 13:58 Comment(6)
@Quartus - 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?Decidua
@Mike possibly because OpenSubKey failed?Quartus
@Quartus 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.Decidua
@Mike You should probably give a look at #13728991Quartus
What if you don't know the name of the key?Relic
@Relic I'm confused - what are you trying to do if you don't know the name of the key?Quartus
A
1

Windows NT

Please do not miss space

Aldwin answered 16/11, 2012 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.