开发者

Parsing registry Binary values to text in .reg/.rgu file format

I want to use C# to write a simple registry value saver for WindowsMobile PPC device to help people to store chosen registry values and recover them after damage or HardReset. My idea was to parse choosen registry keys/values and storing them in a normal .reg file which can then be imported by any registry editor.

I have handled most of the code, but I faced certain problems on parsing some of the values into text. I mean for example binary values represented as Hex.

Sooo I would like to start from something like this:

RegistryKey RgKey = Registry.LocalMachine.OpenSubKey("\Time")
string RgName = "TimeZoneInformation"

string ValueType = Convert.ToString(RgKey.GetValueKind(Rg开发者_JAVA百科Name));
object Value = RgKey.GetValue(RgName);

And "translate" the Value object (which is saved of course in a System.byte[] array) into something like this (I use a string[] array to store values, than after exception testing I put them into file, but you can propose better way):

[HKEY_LOCAL_MACHINE\Time] "TimeZoneInformation"=hex:c4,ff,ff,ff,45,00,75,00,72,00,6f,00,70,00,61,00,20,\ 00,5a,01,72,00,6f,00,64,00,6b,00,6f,00,77,00,61,00,20,00,28,00,63,00,7a,00,61,\ 00,73,00,20,00,73,00,74,00,61,00,6e,00,64,00,2e,00,29,00,00,00,00,00,00,00,00,\ 00,0a,00,00,00,05,00,03,00,00,00,00,00,00,00,00,00,00,00,45,00,75,00,72,00,6f,\ 00,70,00,61,00,20,00,5a,01,72,00,6f,00,64,00,6b,00,6f,00,77,00,61,00,20,00,28,\ 00,63,00,7a,00,61,00,73,00,20,00,6c,00,65,00,74,00,6e,00,69,00,29,00,00,00,00,\ 00,00,00,00,00,00,00,03,00,00,00,05,00,02,00,00,00,00,00,00,00,c4,ff,ff,ff

Can it be done easily? I couldn't find any help on this. Especially that I don't exactly know why the lines in the quoted example (exported by a registry editor) have this certain format. But I tried to mess with this a bit and only this format with backslashes at the end of each line works when actually injected into registry...


After a few nights of thinking (perhaps I'm just not smart enough to do it sooner) I realized that it's a matter of formatting string and not of converting it. And here's what I did:

 RegistryKey RgKey = Registry.LocalMachine.OpenSubKey("\\Time");
 string RgName = "Time";
 string Txt = null;
 string Tmp = null;
     byte[] data = (byte[])RgKey.GetValue(RgName);
     for (int i = 0; i < data.Length; i++)
     {
     if (i % 24 == 0)
         Tmp = Tmp + "\\\n";
     Tmp = Tmp + String.Format("{0:X2}", data[i]);
     if (i < data.Length - 1)
         Tmp = Tmp + ",";
     }
     Txt = RgName + "=hex:" + Tmp;
     break;

It was easier than I thought. I hope this will come in handy to someone else.


Hex values can be any format, but there are 13 predefined known hex formats such as hex(5) which is REG_DWORD_BIG_ENDIAN, to decode all of these hex values use RegtoText, the only tool to decode all hex values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜