We help IT Professionals succeed at work.

Display registry value

dustock
dustock asked
on
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
Comment
Watch Question

Full Stack .NET Developer
Commented:
Hello, I suppose that your OS is 64bit, if you want to read/write on:
SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon

Open in new window

you will need to run/compile your application in 64bit mode, if your aplication is running has a 32bit process then you are reading/writing the values on this key instead:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Winlogon

Open in new window

Carlos VillegasFull Stack .NET Developer

Commented:
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

Author

Commented:
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.

Author

Commented:
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.
Carlos VillegasFull Stack .NET Developer

Commented:
Hello, just set the Platform Target setting on your project properties to "Any CPU" and compile it, example:
project properties
Also the user that will run your application must have administrator privileges on the machine.

Author

Commented:
I got it, thanks for the help!
Carlos VillegasFull Stack .NET Developer

Commented:
Glad to help :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.