0
votes

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!

1
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. - user12031933
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? - user12031933
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... - JHBonarius
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. - DIY Mods
@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 - DIY Mods

1 Answers

0
votes

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.