Writing to the registry (HKEY_LOCAL_MACHINE) in XP
I am trying to change a registry key that I was told controls whether write caching is enabled on certain hard drives. The key should be:HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\IDE\<DiskName>\<SerialNo>\Device Parameters\Disk\UserWriteCacheSetting
However, I am having problems trying to create this key (since it does not exist by default). If I try to open writeable ...\Device Parameters\Disk\
, I get a SecurityException; Msgstr "The requested registry access is not allowed." I have now added a flag <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
to my manifest file to make sure I have admin access, but I still have no 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;
}
source to share
EDIT : Removed the idea of ββpartial trust ... it turned out that it had nothing to do with the problem.
I tried my 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 );
}
}
}
}
source to share