当我尝试将我的密钥保存在注册表中时,我收到错误无法写入注册表项。
//这是我的代码。
注意:我尝试以管理员身份运行,假设某些权限问题仍然出现同样的错误....
private const string RegistryKeyName = "Skms";
private readonly RegistryKey SoftwareKey = Registry.LocalMachine.OpenSubKey("SOFTWARE");
public KeyManagementRegistryKeyChangeImpl(bool writeable)
{
this.writable = writeable;
RegistryKey skms;
if (Environment.Is64BitOperatingSystem == true)
{
skms = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(RegistryKeyName,true);
}
else
{
skms = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
}
if (null == skms)
{
skms = SoftwareKey.CreateSubKey(RegistryKeyName, RegistryKeyPermissionCheck.ReadWriteSubTree);
}
if(skms == null)
{
throw new System.ArgumentException(string.Format(CultureInfo.InvariantCulture,
@"Registry Key 'HKEY_LOCAL_MACHINE\SOFTWARE\{0}' not found or created",
RegistryKeyName));
}
Decryptor decryptor = Decryptor.Create();
答案 0 :(得分:79)
试试这个:
RegistryKey skms = SoftwareKey.OpenSubKey(RegistryKeyName, true);
如果您需要对密钥的写访问权,则应将第二个参数设置为true。
- 编辑 -
在64位系统上,您可以尝试此操作(如果您使用的是.Net 4):
private readonly RegistryKey SoftwareKey =
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).
OpenSubKey("SOFTWARE");
答案 1 :(得分:17)
if (null == skms)
{
skms = Registry.LocalMachine.OpenSubKey("SOFTWARE",true);
RegistryKey key = skms.CreateSubKey(
RegistryKeyName, RegistryKeyPermissionCheck.ReadWriteSubTree);
}
这是我的问题的答案。
答案 2 :(得分:7)
您可能会违反注册表重定向。也许你在64位系统上有一个32位进程,写入HKLM \ Software会被重定向到HKLM \ Software \ Wow6432Node。
您需要直接打开64位密钥,或者为AnyCPU编译。
答案 3 :(得分:3)
using System.Security;
using System.Security.AccessControl;
using Microsoft.Win32;
string user = Environment.UserDomainName + "\\" + Environment.UserName;
RegistryKey rk =
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).
OpenSubKey("SOFTWARE",true);
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule(user,
RegistryRights.WriteKey | RegistryRights.ReadKey | RegistryRights.Delete,
InheritanceFlags.None,
PropagationFlags.None,
AccessControlType.Allow));
rk = Registry.CurrentUser.CreateSubKey("RegistryRightsExample",
RegistryKeyPermissionCheck.Default, rs);