Write a Stringformated Hex Block to registry in Binary value

后端 未结 1 1840
名媛妹妹
名媛妹妹 2021-01-14 19:43

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

相关标签:
1条回答
  • 2021-01-14 19:56

    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());
    }
    
    0 讨论(0)
提交回复
热议问题