在 XP 中写入注册表 (HKEY_LOCAL_MACHINE)

发布于 2024-08-04 11:49:43 字数 1552 浏览 4 评论 0原文

我正在尝试修改一个注册表项,我被告知该注册表项控制是否在特定硬盘驱动器上启用写入缓存。密钥应该是: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\IDE\\\DeviceParameters\Disk\UserWriteCacheSetting

但是,在尝试创建此密钥时,我一直遇到问题(因为默认情况下它不存在)。如果我尝试使用写入权限打开 ...\DeviceParameters\Disk\ ,则会收到 SecurityException 错误; “不允许请求的注册表访问”。现在我已将 标志添加到我的清单文件中,以确保我具有管理员访问权限,但我仍然没有任何权限运气。

任何想法都会很棒!

    static void Main(string[] args)
    {
        RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum\\IDE\\");

        foreach (string driveManafacturer in myKey.GetSubKeyNames())
        {
            RegistryKey driveKey = myKey.OpenSubKey(driveManafacturer);
            foreach (string driveID in driveKey.GetSubKeyNames())
            {
                RegistryKey driveIDKey = driveKey.OpenSubKey(driveID, true);
                string driveType = (string)driveIDKey.GetValue("Class");
                if (driveType == "DiskDrive")
                {
                    RegistryKey tempKey = driveIDKey.OpenSubKey("Device Parameters\\Disk\\", true);
                    if (tempKey == null)
                    {
                        tempKey = driveIDKey.CreateSubKey("Device Parameters\\Disk\\");
                        tempKey.SetValue("UserWriteCacheSetting", 0x0);
                    }
                }
            }
        }

        return;
    }

I am trying to modify a registry key that I have been told controls whether write-caching is enabled on particular hard drives. The key should be: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\IDE\<DiskName>\<SerialNo>\Device Parameters\Disk\UserWriteCacheSetting

However I keep having problems when trying to create this key (as it doesn't exist by default). If I try to open up the ...\Device Parameters\Disk\ with write access I get a SecurityException error; "Requested registry access is not allowed". Now I have added the <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> flag to my manifest file so as to ensure I have admin access, but I'm still not having any luck.

Any ideas would be great!

    static void Main(string[] args)
    {
        RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum\\IDE\\");

        foreach (string driveManafacturer in myKey.GetSubKeyNames())
        {
            RegistryKey driveKey = myKey.OpenSubKey(driveManafacturer);
            foreach (string driveID in driveKey.GetSubKeyNames())
            {
                RegistryKey driveIDKey = driveKey.OpenSubKey(driveID, true);
                string driveType = (string)driveIDKey.GetValue("Class");
                if (driveType == "DiskDrive")
                {
                    RegistryKey tempKey = driveIDKey.OpenSubKey("Device Parameters\\Disk\\", true);
                    if (tempKey == null)
                    {
                        tempKey = driveIDKey.CreateSubKey("Device Parameters\\Disk\\");
                        tempKey.SetValue("UserWriteCacheSetting", 0x0);
                    }
                }
            }
        }

        return;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍景流年 2024-08-11 11:49:43

我没有更好的建议。尝试手动创建注册表项,只是为了确保可以。然后确保应用程序正在使用您的凭据运行。只是为了消除权限问题。

I don't have a better suggestion. Try to create registry entry by hand, just to make sure that you can. Then make sure that the application is running with your credentials. Just to eliminate the permission issue.

秉烛思 2024-08-11 11:49:43

编辑:删除了有关部分信任的想法......事实证明它与问题无关。

我尝试了你的代码并得到了同样的错误 - 经过一些修改它可以工作:

RegistryKey myKey = Registry.LocalMachine.OpenSubKey( "SYSTEM\\CurrentControlSet\\Enum\\IDE\\" );

foreach( string driveManafacturer in myKey.GetSubKeyNames() )
{
  RegistryKey driveKey = myKey.OpenSubKey( driveManafacturer );

  foreach( string driveID in driveKey.GetSubKeyNames() )
  {
    RegistryKey subKey = driveKey.OpenSubKey( driveID );
    string driveType = (string)subKey.GetValue( "Class" );
    if( driveType == "DiskDrive" )
    {
      RegistryKey tempKey = subKey.OpenSubKey( "Device Parameters", true );
      RegistryKey tempKey2 = tempKey.OpenSubKey( "Disk" );
      if( tempKey2 == null )
      {
        tempKey2 = tempKey.CreateSubKey( "Disk" );
        tempKey2.SetValue( "UserWriteCacheSetting", 0x0 );
      }
    }
  }
}

EDIT: Removed the idea about partial trust ... it turned out that it had nothing to do with the problem.

I tried your code and got the same error - with some modifications it works:

RegistryKey myKey = Registry.LocalMachine.OpenSubKey( "SYSTEM\\CurrentControlSet\\Enum\\IDE\\" );

foreach( string driveManafacturer in myKey.GetSubKeyNames() )
{
  RegistryKey driveKey = myKey.OpenSubKey( driveManafacturer );

  foreach( string driveID in driveKey.GetSubKeyNames() )
  {
    RegistryKey subKey = driveKey.OpenSubKey( driveID );
    string driveType = (string)subKey.GetValue( "Class" );
    if( driveType == "DiskDrive" )
    {
      RegistryKey tempKey = subKey.OpenSubKey( "Device Parameters", true );
      RegistryKey tempKey2 = tempKey.OpenSubKey( "Disk" );
      if( tempKey2 == null )
      {
        tempKey2 = tempKey.CreateSubKey( "Disk" );
        tempKey2.SetValue( "UserWriteCacheSetting", 0x0 );
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文