在C#应用程序中写入注册表 [英] Writing to registry in a C# application

查看:583
本文介绍了在C#应用程序中写入注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#应用程序写入注册表。

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

我正在使用此处给出的答案:使用C#将值写入注册表

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.

我正在使用以下代码:

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

Application.name Application.version 文件夹尚不存在。

我必须首先创建它们吗?

Do I have to create them first?

另外,我正在64b Win版本上对其进行测试,所以我认为如果我想检查注册表中添加的密钥,我必须专门检查以下位置的32位注册表:C:\ Windows\SysWOW64\regedit.exe是吗?

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?

推荐答案

首先,如果要在LocalMachine下编辑密钥您必须在管理员权限下运行您的应用程序(最好使用CurrentUser更为安全,或在安装程序中创建密钥)。您还必须在编辑模式下打开键(OpenSubKey方法)以添加新的子键。我已经检查了代码,它可以正常工作。这是代码。

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

这篇关于在C#应用程序中写入注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆