views:

264

answers:

2

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;
    }
+2  A: 

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.

Vadim
Already tried creating it with RegEdit and sure enough it works.. I'm a bit stumped it has to be said.
Siyfion
+1  A: 

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 );
      }
    }
  }
}
tanascius
Good, I'm glad it's not just me having problems then! ;) I'll take a look tomorrow!
Siyfion
I thought that the ClickOnce Security Settings were only in relation to ClickOnce-built installers of your application? Although even after trying both partial trust with RegistryPermission enabled and full trust, the application still throws a SecurityException when trying to open a key with write access.
Siyfion
Hm, I am not really sure about the problem anymore - please have a look at my code - in comparison to yours it runs - with or without ClickOnce security. So it could be the "Device Parameters\\Disk\\" string, that caused the problem. Please give it a try.
tanascius
Very odd, but you're right your modifications allow the code to work as intended. Still not entirely sure why though!? Massive thanks regardless of how it works! ;)
Siyfion