George Shepherd's Windows Forms FAQ
Questions and answers in this FAQ have been collected from newsgroup posts, various mailing lists and the employees of Syncfusion.

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

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


Framework Tips-General IO

© 2001-2009 Copyright Syncfusion Inc. All rights reserved.  |  Privacy Policy  |  Contact  |  Sitemap