Link to home
Start Free TrialLog in
Avatar of dustock
dustockFlag for United States of America

asked on

Display registry value

I am trying to display the value of DefaultPassword from HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon but when ever I try I keep getting "Object reference not set to an instance of an object."  If I change the string value I am trying to get information from to Shell within the same key it works fine.  Can anyone explain why one works and the other doesn't?  I have verified that DefaultPassword is in my registry and I have given it a value or password.


        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                loadReg();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        void loadReg()
        {
            Key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);

            string Val;     
            //Doesn't work
            Val = Key.GetValue("DefaultPassword").ToString();
            
            //Works
            //Val = Key.GetValue("Shell").ToString();
            textBox1.Text = Val;
        }

Open in new window


Thanks,

Dustin
ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I forget to attach the full key name for the first key, I mean:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon

Open in new window

Avatar of dustock

ASKER

I am developing on 64bit Windows 7.  I also set the configuration manager to the x86 platform.  I tried using your suggestion but got the same error.  Eventually this app will run on a Windows XP machine if I can ever get it to read the key.
Avatar of dustock

ASKER

Looks like I tested without verifing the key was present.  it does work with what you gave me.  How will this port to XP though?  The code doesnt currently work on an XP 32bit machine.
Hello, just set the Platform Target setting on your project properties to "Any CPU" and compile it, example:
User generated image
Also the user that will run your application must have administrator privileges on the machine.
Avatar of dustock

ASKER

I got it, thanks for the help!
Glad to help :)