写一个字符串格式的十六进制块,以二进制值注册
本文关键字:十六进制 二进制 注册 格式 字符串 一个 | 更新日期: 2023-09-27 18:17:52
我真的遇到麻烦了。我有一个字符串,其中包括十六进制值:
string creatatorsid =@"01,05年,00 00 00,00,00,05年,15日00,00,00,10,b1, 9 d, 4, 7, 85, 7 b, 05年,79, 05年b3 7 c, ee, 03年,00 00";
我想把它放到需要二进制值的registrykey中。
我试过了:
键。SetValue("creatorSID", creatorSID, RegistryValueKind.Binary);
但是我得到错误信息,它不能被转换。
下面是到目前为止的完整代码:
class Program
{
static void Main(string[] args)
{
String printerName = "Naked";
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM'CurrentControlSet'Control'Print'Printers'" + printerName , true);
string security = "01,00,0c,80,d0,00,00,00,dc,00,00,00,00,00,00,00,14,00,00,00,02," +
"00,bc,00,07,00,00,00,00,00,24,00,0c,00,0f,00,01,05,00,00,00,00,00,05,15,00," +
"00,00,10,b1,9d,4a,7a,85,7b,05,79,05,b3,7c,ee,03,00,00,00,09,24,00,30,00,0f," +
"00,01,05,00,00,00,00,00,05,15,00,00,00,10,b1,9d,4a,7a,85,7b,05,79,05,b3,7c," +
"ee,03,00,00,00,09,14,00,00,00,00,10,01,01,00,00,00,00,00,03,00,00,00,00,00," +
"00,14,00,08,00,02,00,01,01,00,00,00,00,00,01,00,00,00,00,00,0a,14,00,00,00," +
"00,20,01,01,00,00,00,00,00,01,00,00,00,00,00,00,18,00,0c,00,0f,00,01,02,00," +
"00,00,00,00,05,20,00,00,00,20,02,00,00,00,0b,18,00,00,00,00,10,01,02,00,00," +
"00,00,00,05,20,00,00,00,20,02,00,00,01,01,00,00,00,00,00,05,12,00,00,00,01," +
"01,00,00,00,00,00,05,12,00,00,00";
var data = Encoding.Unicode.GetBytes(security);
try
{
key.SetValue("Security", data, RegistryValueKind.Binary);
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
Console.WriteLine(exc.StackTrace);
}
}
}
现在正在写入条目,但是值发生了变化。如果我检查键有:"30 00 31 00 ....."在它。但它一定是:"01 000 c 80....."(必须是字符串中的值)
当您使用string时,您必须使用RegistryValueKind.String
。
key.SetValue("creatorSID", creatorSid , RegistryValueKind.String);
如果您需要使用RegistryValueKind.Binary
,则需要将string
转换为byte[]
并将其存储为byte[]
。
存储值:
var data = Encoding.Unicode.GetBytes(creatorSid);
key.SetValue("creatorSID", data, RegistryValueKind.Binary);
返回值:
var data = (byte[])key.GetValue("creatorSID");
if (data != null)
{
string creatorSID = Encoding.Unicode.GetString(data);
}
RegistryKey。SetValue
Edit:似乎您不需要将,
字符转换为字节。
存储值:
var data = security.Split(',')
.Select(x => Convert.ToByte(x, 16))
.ToArray();
key.SetValue("creatorSID", data, RegistryValueKind.Binary);
返回值:
var data = (byte[])key.GetValue("creatorSID");
if (data != null)
{
string creatorSID = string.Join(",", data.Select(x => x.ToString("x2")).ToArray());
}