c# - 如何在没有管理员权限的情况下读写注册表_C#

标签 c# registry privileges administration

我需要在没有管理员模式的情况下在注册表中写入和读取 key 。首先我想知道这是否可行?

请找到下面的代码(它工作正常,当 Visual Studio 在管理员模式下打开时)

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("myval", "677");
key.Close();  

阅读

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);

  key.CreateSubKey("AppName");
  key = key.OpenSubKey("AppName", true);
   //if it does exist, retrieve the stored values  
  if (key != null)
  {
    string s = (string)key.GetValue("myval");
    Console.Read();
    key.Close();
   }  

请任何人提出一种方法来做到这一点。 (我也尝试添加下面的行)

错误:

ClickOnce does not support the request execution level 'requireAdministrator'.

最佳答案

这里有一个答案 how to write in registry without admin

读取是通过使用 RegistryKeyPermissionCheck.ReadSubTree 选项完成的

    RegistryKey baseRegistryKey = RegistryKey.OpenBaseKey(baseKey, RegistryView.Registry64);
    RegistryKey subRegistryKey = baseRegistryKey.OpenSubKey(subKey, RegistryKeyPermissionCheck.ReadSubTree);
    if (subRegistryKey != null)
    {
        object value64 = subRegistryKey.GetValue(value);
        if (value64 != null)
        {
            baseRegistryKey.Close();
            subRegistryKey.Close();
            return value64;
        }
        subRegistryKey.Close();
    }            
    baseRegistryKey.Close();

然后对 RegistryView.Registry32 做同样的事情

关于c# - 如何在没有管理员权限的情况下读写注册表_C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50837139/

相关文章:

c# - 在 C# 中奇怪地执行 get 访问器?

c# - 如何获得给定索引的二维数组中的最小值?

c# - 自动实现的 getter 和 setter 与公共(public)字段

c# - 如何检测注册表项是否有错误 C#

c# - 如何: Create a Key In the Registry (Visual C#)?

windows - 在 Windows 下以访客身份或以有限权限运行应用程序而无需重新登录?

applescript - Mac applescript - 要求管理员权限提示

c# - Azure存储错误: "An attempt was made to access a socket in a way forbidden by its access permissions."

vb6 - 从 Windows 注册表读取值?

vbscript - 使用VBscript读取用户权限