public void SetValue (RegistryKey rkey, string name, object value)
{
Type type = value.GetType ();
int result;
IntPtr handle = GetHandle (rkey);
if (type == typeof (int)) {
int rawValue = (int)value;
result = RegSetValueEx (handle, name, IntPtr.Zero, RegistryValueKind.DWord, ref rawValue, Int32ByteSize);
} else if (type == typeof (byte[])) {
byte[] rawValue = (byte[]) value;
result = RegSetValueEx (handle, name, IntPtr.Zero, RegistryValueKind.Binary, rawValue, rawValue.Length);
} else if (type == typeof (string[])) {
string[] vals = (string[]) value;
StringBuilder fullStringValue = new StringBuilder ();
foreach (string v in vals)
{
fullStringValue.Append (v);
fullStringValue.Append ('\0');
}
fullStringValue.Append ('\0');
byte[] rawValue = Encoding.Unicode.GetBytes (fullStringValue.ToString ());
result = RegSetValueEx (handle, name, IntPtr.Zero, RegistryValueKind.MultiString, rawValue, rawValue.Length);
} else if (type.IsArray) {
throw new ArgumentException ("Only string and byte arrays can written as registry values");
} else {
string rawValue = String.Format ("{0}{1}", value, '\0');
result = RegSetValueEx (handle, name, IntPtr.Zero, RegistryValueKind.String, rawValue,
rawValue.Length * NativeBytesPerCharacter);
}
if (result == Win32ResultCode.MarkedForDeletion)
throw RegistryKey.CreateMarkedForDeletionException ();
// handle the result codes
if (result != Win32ResultCode.Success)
{
GenerateException (result);
}
}