I have a number of distributed sites that need to have a registry
setting in order to stop serenum incorrectly identifying our serial
device as a mouse etc.
I have written the following code in an attempt to change the registry
settings, but a SecurityException is thrown with message 'Requested
registry access is not allowed'. This is despite being run in an
administrator account in Windows XP. The exception is known in the
first line of code in which I try to access a registry key. Can anyone
help me to find a way to programmatically change these settings
please?
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Security.Permissions;
using System.Security.AccessControl;
[assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name
= "FullTrust")]
namespace serenum
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Checking Registry...");
RegistryPermission permission = new
RegistryPermission(RegistryPermissionAccess.Write,
"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\ACPI\\PNP0501");
permission.Demand();
RegistryKey key =
Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum\\ACPI\\PNP0501",
true);
foreach (string keyName in key.GetSubKeyNames())
{
RegistryKey portKey = key.OpenSubKey(keyName, true);
RegistryKey parmKey = portKey.OpenSubKey("Device
Parameters", true);
string portName = "";
string skipEnumerations = "";
bool skipEnumerationsFound = false;
foreach (string valName in parmKey.GetValueNames())
{
object v = parmKey.GetValue(valName);
RegistryValueKind rvk =
parmKey.GetValueKind(valName);
if (valName == "PortName")
portName = v.ToString();
else if (valName == "SkipEnumerations")
{
skipEnumerations = v.ToString();
skipEnumerationsFound = true;
}
}
if (portName != "")
{
if (skipEnumerationsFound)
Console.WriteLine(portName + ": " +
skipEnumerations);
else
Console.WriteLine(portName + ": No key
found");
}
if (skipEnumerations == "")
{
//RegistryPermission f = new
RegistryPermission(RegistryPermissionAccess.AllAccess,
//
"SYSTEM\\CurrentControlSet\\Enum\\ACPI\\PNP0501\\" + keyName +
"\\SkipEnumerations");
parmKey.SetValue("SkipEnumerations", 0xfffffffe,
RegistryValueKind.DWord);
}
}
Console.WriteLine("Finished");
Console.ReadKey();
}
}
}