2

I want to add/edit registry keys through the C# app.

I tried this, but it doesn't add the key:

    Microsoft.Win32.RegistryKey key;
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Serialize");
            key.SetValue("Startupdelayinmsec", "dword:00000000");
            key.Close();

        }

the code should add this key:

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Serialize] "Startupdelayinmsec"=dword:00000000

I'm not sure, but possibly it's because it's 64 system while I use Microsoft.Win32.RegistryKey.

Thanks in advance for the help!

9
  • Thank you for taking the time to share your problem. But the bug can't be reproduced and the code you provided seems to work fine. Please try to better explain your issue, your development environment and the data structures, as well as to share more code (no screenshot), images or sketches of the screen, and user stories or scenario diagrams. To help you improve your requests, please read the How do I ask a good question and Questions I avoid asking at the top right. Commented Nov 8, 2019 at 19:43
  • It works on Windows 7 x64 Home with admin user. Perhaps a policy problem? What are your windows user account type and permissions? Do you get key as null or an exception on SetValue? Are you sure that the key and the value are not created? Commented Nov 8, 2019 at 19:44
  • You need elevated permissions to be able to modify the registry (else any app could just screw with you essential system. i.e. giant security issue). But you are not showing enough code to see what you are doing... Commented Nov 8, 2019 at 19:52
  • Thank you for your answers, guys! I'm using a Win 10 64 from the administrator account. The app is WPF C#. There is not much code, the snippet from the question inserted in the button click event and it's pretty much it. There are no errors, code compiles and does all other required actions except this one. Commented Nov 8, 2019 at 19:56
  • @JHBonarius As I said above, it's just an app with labels and several buttons that allow users to add/modify registry keys by clicking the corresponding button. Here is the code, but I doubt it will tell more pastebin.com/5hqd4CwE Commented Nov 8, 2019 at 20:00

1 Answer 1

3

First, does the call "fail" or does it succeed but you do not see the key afterwards? If any of the calls in the code you've shown fail, they should be throwing an exception.

Second, you've got the syntax for the SetValue call wrong. The second parameter is supposed to be the object to set the value to, not a string representing the value and its type which is what you look to be doing. In other words, change it to either this:

key.SetValue("Startupdelayinmsec", 0);

or if you want to specify the exact type of the new value, then this:

key.SetValue("Startupdelayinmsec", 0, RegistryValueKind.DWord);

Also, as others have stated in comments, you will need rights to change the registry. Logging in as an admin may not be enough. You may need to run your app as an admin also.

Sign up to request clarification or add additional context in comments.

3 Comments

Perfect! It works now. The issue was the type of value. Thanks a lot!
Do you know by any chance how to delete the key/set to the default value?
To Delete the key use the DeleteSubKey method on the CurrentUser (similar to your use of CreateSubKey). To delete the Value use key.DeleteValue("Startupdelayinmsec"). I don't know that there is a default value on registry values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.