|
66.1 How do you persist any object in the registry
|
 |
To store an object in the registry, the object should be serializable (either has a Serializable attribute attached to it or derives from ISerializable; same holds to all contained objects).
|
ArrayList names; // Source object; Can contain any object that is serializable
|
... // Fill up this arraylist
|
BinaryFormatter formatter = new BinaryFormatter();
|
MemoryStream stream1 = new MemoryStream();
|
formatter.Serialize(stream1, names);
|
... // Open the key where you want to store it, with write permissions
|
regKey.SetValue("ValueName", stream1.ToArray());
|
ArrayList names; // Destination object
|
... // Open the corresponding key
|
BinaryFormatter formatter = new BinaryFormatter();
|
MemoryStream stream1 = new MemoryStream();
|
barray1 = (byte[])regKey.GetValue("ValueName");
|
stream1.Write(barray1, 0, barray1.Length);
|
MemoryStream stream1 = new MemoryStream();
|
barray1 = (byte[])regKey.GetValue("ValueName");
|
stream1.Write(barray1, 0, barray1.Length);
|
names = formatter.Deserialize(stream1) as ArrayList;
|
|
Framework Tips-General IO
|
|
|