Writing to registry in a C# application

前端 未结 5 1015
离开以前
离开以前 2020-12-04 14:17

I\'m trying to write to the registry using my C# app.

I\'m using the answer given here: Writing values to the registry with C#

However for some reason the ke

5条回答
  •  佛祖请我去吃肉
    2020-12-04 14:50

    First of all if you want to edit key under LocalMachine you must run your application under admin rights (better use CurrentUser it's safer or create the key in installer). You have to open key in edit mode too (OpenSubKey method) to add new subkeys. I've checked the code and it works. Here is the code.

    RegistryKey key = Registry.LocalMachine.OpenSubKey("Software",true);
    
    key.CreateSubKey("AppName");
    key = key.OpenSubKey("AppName", true);
    
    
    key.CreateSubKey("AppVersion");
    key = key.OpenSubKey("AppVersion", true);
    
    key.SetValue("yourkey", "yourvalue");
    

提交回复
热议问题