C# 中不允许请求的注册表访问

时间:2021-04-24 08:19:30

标签: c#

我正在做一个小项目,它可以帮助我在全新安装后自动配置窗口。不知道为什么我无法更改或删除本地计算机中的某些键。

我可以从 CurrentUser 中删除一些子项,但从 Local Machine 中我没有。

有人可以帮我吗?

private static void SwapFile()
{
  RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management", true);
  key.DeleteSubKey("ClearPageFileAtShutdown");
  key.Close();
}

这个有效。

private static void AutoStartCleaner()
{
  RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
  key.DeleteValue("OneDrive");
  key.Close();
}

这是控制台的输出:

Unhandled Exception: System.Security.SecurityException: Requested registry access is not allowed.
at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at ConsoleApp1.Program.SwapFile() in C:\Users\test\Desktop\1\cc\ConsoleApp1\ConsoleApp1\Program.cs:line 23
at ConsoleApp1.Program.Main(String[] args) in C:\Users\test\Desktop\1\cc\ConsoleApp1\ConsoleApp1\Program.cs:line 16
Press any key to continue...

我想用 C# 代码将 ClearPageFileAtShutdown 的值设置为从 0 到 1。

0 个答案:

没有答案