Writing to registry in a C# application

前端 未结 5 1011
离开以前
离开以前 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:43

    You can use the following code to create and open the required registry keys.

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

    You can basically use CreateSubKey for all your application settings, as it will open the key for write access, if it already exists, and create it otherwise. There is no need to create first, and then open. OpenSubKey comes in handy when you are absolutely certain the key already exists, like in this case, with "HKEY_LOCAL_MACHINE\SOFTWARE\"

提交回复
热议问题