DeviceRegistryKey.GetValueNames() returns same strings with different indexes each time.

Feb 6, 2013 at 9:54 AM
I open a subkey (\comm\tcpip\parms) of Local_Machine registry. I am sure there is no further subkey in the hierarchy. I call DeviceRegistryKey.GetValueNames() to get string array of names.
I get values for each returned string on the click event of button1. Its oK. Now I set some values on the click event of button2 and closes the open subkey. Again, to make sure, I click button1 to get changed values. This time OpenSubKey() function is called and GetValueNames() returns array of names but with different indexes i.e. First time "EnableDHCP" was at index 2, this time it is at index 1. It ruins my programming logic.
        RemoteDevice rd;
        string[] temp;
        RemoteDevice.DeviceRegistryKey rk;
        private void button1_Click(object sender, EventArgs e)
            rd = mgr.Devices.FirstConnectedDevice;
            rk = rd.DeviceRegistryLocalMachine.OpenSubKey("\\Comm\\Tcpip\\Parms");
            temp = rk.GetValueNames();
            object obj = rk.GetValue(temp[0],null);
            object obj1 = rk.GetValue(temp[1], null);

        private void button2_Click(object sender, EventArgs e)

            string[] temp = rk.GetValueNames();
            rk.SetValue(temp[0], 0);
            rk.SetValue(temp[1], 16384);
First click button1 then button2. Again Clicking on button1 gives different value of "temp" variable. So it does not read expected value.

Please help
Feb 7, 2013 at 6:24 PM
I would try the "using" clause around all the use of an open key to automatically close it within each of the button click methods.