Writing binary data to registry

  • Thread starter Thread starter Dmitry
  • Start date Start date
D

Dmitry

Hi!
Does anybody know how to write binary data to registry?
The purpose is to store an boxed object into registry and to
restore it.
 
Dmitry said:
Does anybody know how to write binary data to registry?
The purpose is to store an boxed object into registry and to
restore it.

I believe you can use RegistryKey.SetValue, passing in a byte array
containing the appropriate data.
 
Thanks, I did that!
IFormatter formatter = new BinaryFormatter();

// ?????? ??? ???????????? ? ??????

byte[] mas = new byte[1024];

Stream stream = new MemoryStream(mas);

formatter.Serialize(stream, obj);

long i = stream.Position;

stream.Close();

byte[] mas2 = new Byte;

// ????????? ???????? ?????? ?? mas ? mas2

for(int j = 0; j < i; j++)

{

mas2[j] = mas[j];

}

and there is a next problem.

how to calulate an accurate size of needed array (mas) ?



CurrentKey.SetValue(Name, mas2);
 
and there is a next problem.

how to calulate an accurate size of needed array (mas) ?

You don't. Most of the code you posted isn't necessary. Instead, do:

Stream stream = new MemoryStream();
formatter.Serialize (stream, obj);
stream.Close();
byte[] data = stream.ToArray();
 
thanks again.
th? working code is:
MemoryStream stream = new MemoryStream();

formatter.Serialize(stream, obj);

stream.Close();

byte[] mas = stream.ToArray();

Jon Skeet said:
and there is a next problem.

how to calulate an accurate size of needed array (mas) ?

You don't. Most of the code you posted isn't necessary. Instead, do:

Stream stream = new MemoryStream();
formatter.Serialize (stream, obj);
stream.Close();
byte[] data = stream.ToArray();
 
Back
Top