I am really get stucked with something. I have a string which includes hex values:
string creatorSid = @\"01,05,00,00,00,00,00,05,15,00,00,00,10,b1
When you have string, you've to use RegistryValueKind.String
.
key.SetValue("creatorSID", creatorSid , RegistryValueKind.String);
If at all you need to use RegistryValueKind.Binary
, you need to convert the string
to byte[]
and store it as byte[]
.
To store the value:
var data = Encoding.Unicode.GetBytes(creatorSid);
key.SetValue("creatorSID", data, RegistryValueKind.Binary);
To get the value back:
var data = (byte[])key.GetValue("creatorSID");
if (data != null)
{
string creatorSID = Encoding.Unicode.GetString(data);
}
RegistryKey.SetValue
Edit: It seems you don't need to convert the ,
character as byte. Try the following.
To store the value:
var data = security.Split(',')
.Select(x => Convert.ToByte(x, 16))
.ToArray();
key.SetValue("creatorSID", data, RegistryValueKind.Binary);
To get the value back:
var data = (byte[])key.GetValue("creatorSID");
if (data != null)
{
string creatorSID = string.Join(",", data.Select(x => x.ToString("x2")).ToArray());
}