How do you persist any object in the registry

Platform: WinForms| Category: General IO

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);

	RegistryKey regKey;
	... // Open the key where you want to store it, with write permissions
	regKey.SetValue('ValueName', stream1.ToArray());
	
	To Read from registry:
	ArrayList names;	// Destination object
	RegKey regKey;
	...	// Open the corresponding key

	BinaryFormatter formatter = new BinaryFormatter();
	MemoryStream stream1 = new MemoryStream();
	
	byte[] barray1 = null;
	barray1 = (byte[])regKey.GetValue('ValueName');
	if(barray1 != null)
	{
		stream1.Write(barray1, 0, barray1.Length);
		MemoryStream stream1 = new MemoryStream();
	
		byte[] barray1 = null;
		barray1 = (byte[])regKey.GetValue('ValueName');
		if(barray1 != null)
		{
			stream1.Write(barray1, 0, barray1.Length);
			stream1.Position = 0;
		names = formatter.Deserialize(stream1) as ArrayList;
	}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.