我正在尝试修改一个注册表项,我被告知控制是否在特定硬盘驱动器上启用了写入缓存。关键应该是:HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\IDE\<DiskName>\<SerialNo>\Device Parameters\Disk\UserWriteCacheSetting
但是我在尝试创建此密钥时遇到问题(因为默认情况下不存在)。如果我尝试使用写访问权限打开...\Device Parameters\Disk\
,我会收到SecurityException错误; “不允许请求注册表访问”。现在我已将<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
标志添加到我的清单文件中,以确保我有管理员权限,但我仍然没有运气。
任何想法都会很棒!
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;
}
答案 0 :(得分:2)
我没有更好的建议。尝试手动创建注册表项,以确保您可以。然后确保应用程序与您的凭据一起运行。只是为了消除许可问题。
答案 1 :(得分:1)
编辑:删除了部分信任的想法......事实证明它与问题无关。
我尝试了你的代码并得到了同样的错误 - 通过一些修改它可以工作:
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 );
}
}
}
}