Writing to registry in a C# application

☆樱花仙子☆ 提交于 2019-12-28 08:08:11

问题


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 key isn't added to the registry.

I'm using the following code:

string Timestamp = DateTime.Now.ToString("dd-MM-yyyy");

string key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\"+Application.ProductName+"\\"+Application.ProductVersion;
string valueName = "Trial Period";

Microsoft.Win32.Registry.SetValue(key, valueName, Timestamp, Microsoft.Win32.RegistryValueKind.String);

The Application.name and Application.version 'folders' don't exists yet.

Do I have to create them first?

Also, I'm testing it on a 64b Win version so I think if I want to check the registry for the key added I have to specifically check the 32bit registry in: C:\Windows\SysWOW64\regedit.exe don't I?


回答1:


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");



回答2:


Also check if your registry calls are getting virtualised. See here for more information.

It can happen if your application is not UAC aware and occurs for compatibility reasons.

Real path
HKEY_LOCAL_MACHINE\Software\FooKey

Virtual path
HKEY_USERS\<User SID>_Classes\VirtualStore\Machine\Software\FooKey



回答3:


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\"




回答4:


Try to open HKLM\Software first. Then create key for your program, and then create key for version. Howewer, your key could be placed at HKLM\software\WOW6432Node. Check this.




回答5:


The problem is you don't have enough privileges. Here is a way that works for my:

RegistryKey myKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
myKey = myKey.OpenSubKey(subkey, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);

if (myKey != null)
{
    myKey.SetValue("DefaultPrinterId", ldiPrinters[e.RowIndex].id, RegistryValueKind.String);
    myKey.Close();
}

With RegistryKey.OpenBaseKey you open the correct registry, because when you don't have permissions the registry that you write, it does in another location.



来源:https://stackoverflow.com/questions/7230102/writing-to-registry-in-a-c-sharp-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!