Reading Registry using C# (System.Byte is 'binary' data value)

  • Thread starter Thread starter John Hoffman
  • Start date Start date
J

John Hoffman

Reading registry:

....
RegistryKey rksub = rkey.OpenSubKey(s);
String [] valstr = rksub.GetValueNames();
foreach (String vs in valstr)
{
String vstr = rksub.GetValue(vs).ToString();
OR
String vstr = rksub.GetValue(vs);
...

call to GetValue(vs) returns "System.Byte" string if value is reg binary
entry (instead of string or DWORD).
How do you get to actual byte buffer with reg entry value returned as
"System.Byte"?
Thanks
 
Ok,
Figured out you can cast as follows:

System.Byte[] strByte = (System.Byte[]) rksub.GetValue(vs); // This
returns buffer (say 16 byte buffer...) in strByte

What is the C# way to convert contents of strByte into a string? (C++ would
be the usual.....)
OR, is there a way to even bypass casting step? Strings, strings, string....
Thanks for any help!!!
 
Exception. Didn't work. Thanks


Robert Misiak said:
Try casting it:

string vstr = (string)rksub.GetValue(vs);

Robert


John Hoffman said:
Reading registry:

...
RegistryKey rksub = rkey.OpenSubKey(s);
String [] valstr = rksub.GetValueNames();
foreach (String vs in valstr)
{
String vstr = rksub.GetValue(vs).ToString();
OR
String vstr = rksub.GetValue(vs);
...

call to GetValue(vs) returns "System.Byte" string if value is reg binary
entry (instead of string or DWORD).
How do you get to actual byte buffer with reg entry value returned as
"System.Byte"?
Thanks
 
Figured out you can cast as follows:

System.Byte[] strByte = (System.Byte[]) rksub.GetValue(vs); // This
returns buffer (say 16 byte buffer...) in strByte

What is the C# way to convert contents of strByte into a string? (C++ would
be the usual.....)
OR, is there a way to even bypass casting step? Strings, strings, string....
Thanks for any help!!!

I have to wonder why you've got a string value in a binary entry to
start with. However, if you have, you just need to do the same as any
other byte[] to string conversion - use Encoding.

See http://www.pobox.com/~skeet/csharp/unicode.html
 
I don't have a string. I was just trying how to read in binary data (not
DWORD, not string data) from Registry. C++ is easy. I just noticed that the
debugger in C# tipped it was a System.Byte[]. When I looked to System.Byte
msdn refs, not clear (or maybe I accept I'm brain dead) how to use C# fric'n
syntax to read binary buffer using .NET. Casting? System.Array?
System.Buffer? What do the experts do? That's all. Thanks for effort and
sorry for my lack of patience. It's just that all 'expert' examples at <you
name it web site> show all the DWORD and string examples. Guess they forgot
about binary data in the registry.


Jon Skeet said:
Figured out you can cast as follows:

System.Byte[] strByte = (System.Byte[]) rksub.GetValue(vs); // This
returns buffer (say 16 byte buffer...) in strByte

What is the C# way to convert contents of strByte into a string? (C++ would
be the usual.....)
OR, is there a way to even bypass casting step? Strings, strings, string....
Thanks for any help!!!

I have to wonder why you've got a string value in a binary entry to
start with. However, if you have, you just need to do the same as any
other byte[] to string conversion - use Encoding.

See http://www.pobox.com/~skeet/csharp/unicode.html
 
Sorry, I am brain dead. I was reading in binary data to a System.Byte[]
variable. I was just having problems formatting some of the byte data into a
string to write out. Encoding is the answer. Thanks, and good night!

Jon Skeet said:
Figured out you can cast as follows:

System.Byte[] strByte = (System.Byte[]) rksub.GetValue(vs); // This
returns buffer (say 16 byte buffer...) in strByte

What is the C# way to convert contents of strByte into a string? (C++ would
be the usual.....)
OR, is there a way to even bypass casting step? Strings, strings, string....
Thanks for any help!!!

I have to wonder why you've got a string value in a binary entry to
start with. However, if you have, you just need to do the same as any
other byte[] to string conversion - use Encoding.

See http://www.pobox.com/~skeet/csharp/unicode.html
 
Back
Top