将二进制格式的字符串格式的十六进制块写入注册表 [英] Write a Stringformated Hex Block to registry in Binary value
问题描述
我真的被某些东西卡住了. 我有一个包含十六进制值的字符串:
I am really get stucked with something. I have a string which includes hex values:
字符串creatorSid = @"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;
string creatorSid = @"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";
我想将其放入需要二进制值的注册表项中.
I want to put that into the registrykey which requires binary values.
我尝试过:
key.SetValue("creatorSID",creatorSid,RegistryValueKind.Binary);
key.SetValue("creatorSID", creatorSid , RegistryValueKind.Binary);
但是我收到错误消息,说它无法转换.
But I got error message that it can not be converted.
这是到目前为止的完整代码:
Here is the complete Code so far:
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 00 0c 80 ....."(必须恰好是字符串中的值)
It`s writing the entry now, but the values are changed. If i check the key there is: "30 00 31 00 ....." in it. But it must be: "01 00 0c 80....." (must be exactly the value from the string)
推荐答案
拥有字符串时,必须使用RegistryValueKind.String
.
When you have string, you've to use RegistryValueKind.String
.
key.SetValue("creatorSID", creatorSid , RegistryValueKind.String);
如果根本需要使用RegistryValueKind.Binary
,则需要将string
转换为byte[]
并将其存储为byte[]
.
If at all you need to use RegistryValueKind.Binary
, you need to convert the string
to byte[]
and store it as 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);
}
:您似乎不需要将,
字符转换为字节.请尝试以下操作.
It seems you don't need to convert the ,
character as byte. Try the following.
要存储值:
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());
}
这篇关于将二进制格式的字符串格式的十六进制块写入注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!