我想为注册表项设置值。当我尝试使用SetValue方法时,它会抛出 UnauthorizedAccessException 异常,即:
尝试执行未经授权的操作。
这是我的代码:
var key = Microsoft.Win32.Registry.CurrentUser
.OpenSubKey("SOFTWARE")
.OpenSubKey("Microsoft")
.OpenSubKey("Windows")
.OpenSubKey("CurrentVersion")
.OpenSubKey("Run", true);
if (key.GetValue("MyKey") == null)
{
key.SetValue("MyKey", localPath, Microsoft.Win32.RegistryValueKind.String);
}
答案 0 :(得分:0)
为什么不尝试使用Registry
Class?
RegistryKey registry = Registry.LocalMachine.OpenSubKey(@"software\newclient", true);
if (registry == null)
{
Registry.LocalMachine.CreateSubKey(@"software\newclient");
registry = Registry.LocalMachine.OpenSubKey(@"software\newclient", true);
}
if (registry.GetValue("CreatedTime")==null)
{
registry.SetValue("CreatedTime", "1/1/1900 00:00 PM");
}
if (registry.GetValue("CurrentVersion") == null)
{
registry.SetValue("CurrentVersion", "0.0.0.0");
}
if (Convert.ToDateTime(registry.GetValue("CreatedTime").ToString()) != Convert.ToDateTime(arr[1]))
{
/*
* other code snippet
*
*/
registry.SetValue("CurrentVersion",arr[0]);
registry.SetValue("CreatedTime", arr[1]);
return true;
}
registry.Close();
return false;
答案 1 :(得分:0)
OpenSubKey 可以带2个参数,其中一个是子键路径,第二个可以带可写(bool)参数
@AppStorage("History") var history: [String] = ["End of history"]