locked
How to create key in registry RRS feed

  • Question

  • Hello,

    I am trying to create 2 Keys in the registry. The 2 keys I like to create are the ones seen in this picture:

    registry image

    As seen they are of type "REG_DWORD" and have the values: 60000 and 30. The paths for the keys are those:

    "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\TcpTimedWaitDelay"
    "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\MaxUserPort"

    The code I have tested is the below but this does not create the keys at all. I also wonder what to put as parameter "value" in this case?

    Thank you!

                String p1 = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\TcpTimedWaitDelay";
                String p2 = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\MaxUserPort";
    
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(p1);
                key.SetValue("value", "30");
                key.Close();
    
    
                key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(p2);
                key.SetValue("value", "60000");
                key.Close();  


    • Edited by Silvers2 Tuesday, May 31, 2016 8:12 PM
    Tuesday, May 31, 2016 8:11 PM

Answers

  • Hi,

    sorry, that way my fault. I just copied code parts of your code and didn't check the used types with the requirements.

    I cannot test it right now but I would expect that you get the correct type if you use

    String path = "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters";
    
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(path, true))
                {
                    key.SetValue("TcpTimedWaitDelay", 30);
                    key.SetValue("MaxUserPort", 60000);
                }

    So instead of using strings we hand over the integer values.

    With kind regards,

    Konrad

    • Marked as answer by Silvers2 Sunday, June 5, 2016 12:50 PM
    Friday, June 3, 2016 5:19 AM

All replies

  • Hi,

    I see multiple points. The first important point is the core understanding of the registry key concept with all the values stored inside the keys.

    The next part is the understanding of the Registry Hives.

    So RegistryKey is what you might see as "folder" inside regedit. And the single values are the core values. So you want to open the key "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters" and have 2 values in there. And you want to access the HKey_Local_Machine hive, so you should use that in your code, too.

    So you use Registry.LocalMachine instead of Registry.CurrentUser!

    And the key itself must exist (else TcpIp is not installed, it does not make sense to create the key), so I would suggest to use OpenSubKey instead of CreateSubKey.

    You are already at the root of the hive, so you just need a relativ path:
    "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters"

    So the code should be something like this:

                String path = "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters";
    
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(path))
                {
                    key.SetValue("TcpTimedWaitDelay", "30");
                    key.SetValue("MaxUserPort", "60000");
                }
    

    With kind regards,

    Konrad

    • Proposed as answer by DotNet Wang Thursday, June 2, 2016 2:41 AM
    Tuesday, May 31, 2016 9:15 PM
  • Thank you, then I understand better and I use the Registry.LocalMachine.

    I noticed that it was needed to add true for it to writable like below. 

    However, when I used the code the key changed from "REG_DWORD" to "REG_SZ" as type. I wonder if that doesn't matter or what that can mean?

    String path = "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters";
    
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(path, true))
                {
                    key.SetValue("TcpTimedWaitDelay", "30");
                    key.SetValue("MaxUserPort", "60000");
                }

    Thursday, June 2, 2016 11:36 PM
  • Hi,

    sorry, that way my fault. I just copied code parts of your code and didn't check the used types with the requirements.

    I cannot test it right now but I would expect that you get the correct type if you use

    String path = "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters";
    
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(path, true))
                {
                    key.SetValue("TcpTimedWaitDelay", 30);
                    key.SetValue("MaxUserPort", 60000);
                }

    So instead of using strings we hand over the integer values.

    With kind regards,

    Konrad

    • Marked as answer by Silvers2 Sunday, June 5, 2016 12:50 PM
    Friday, June 3, 2016 5:19 AM
  • That is great, I changed it to integer values and it worked out.

    Thank you very much for your help

    Sunday, June 5, 2016 12:50 PM