Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 20th, 2012  |  syntax: None  |  size: 2.48 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C#, can't serialize to binary
  2. [Serializable]
  3. public class Settings : ISerializable
  4. {
  5.     public static string server= "http://localhost/";
  6.     public static string username = "myname";
  7.     public static bool savePassword = true;
  8.     public static bool autoSync = true;
  9.     public static string password = "mypass";
  10.     public static string folderPath1= "c:/";
  11.     public static string folderPath2= "c:/";
  12.     public static string autoSyncDuration = "300";
  13.     public static string lastSyncTime = "???";
  14.  
  15.  
  16.     public Settings()
  17.     { }
  18.  
  19.     public void GetObjectData(SerializationInfo info, StreamingContext context)
  20.     {
  21.         Type myTypeObj = Type.GetType("Settings");
  22.         foreach (FieldInfo p in myTypeObj.GetFields())
  23.         {
  24.             Object value = p.GetValue(null);
  25.             info.AddValue(p.Name, value, p.GetType());
  26.         }
  27.     }
  28.  
  29.     public Settings(SerializationInfo info, StreamingContext context)
  30.     {
  31.         Type myTypeObj = Type.GetType("Settings");
  32.         FieldInfo p;
  33.         foreach (SerializationEntry e in info)
  34.         {
  35.             p = myTypeObj.GetField(e.Name);
  36.             p.SetValue(null, e.Value);
  37.         }
  38.     }
  39. }
  40.        
  41. private void writeSettings()
  42.     {
  43.         pcb_savingSettings.Visible = true;
  44.         FileStream fileStream = new FileStream(settingFile, FileMode.Create, FileAccess.Write, FileShare.None);
  45.         BinaryFormatter bf = new BinaryFormatter();
  46.         bf.Serialize(fileStream, new Settings());
  47.  
  48.         fileStream.Close();
  49.         pcb_savingSettings.Visible = false;
  50.     }
  51.     private void readSettings()
  52.     {
  53.         if (!File.Exists(settingFile))
  54.         {
  55.             writeSettings();
  56.         }
  57.         FileStream fileStream = new FileStream(settingFile, FileMode.Open, FileAccess.Read, FileShare.None);
  58.         BinaryFormatter bf = new BinaryFormatter();
  59.         bf.Deserialize(fileStream);
  60.         fileStream.Close();
  61.     }
  62.        
  63. bf.Deserialize(fileStream);
  64.        
  65. public void GetObjectData(SerializationInfo info, StreamingContext context)
  66. {
  67.     Type myTypeObj = Type.GetType("Settings");
  68.     foreach (FieldInfo p in myTypeObj.GetFields())
  69.     {
  70.         Object value = p.GetValue(null);
  71.         info.AddValue(p.Name, value, p.GetType());
  72.     }
  73. }
  74.        
  75. public void GetObjectData(SerializationInfo info, StreamingContext context)
  76.     {
  77.         Type myTypeObj = Type.GetType("Settings");
  78.         foreach (FieldInfo p in myTypeObj.GetFields())
  79.         {
  80.             Object value = p.GetValue(null);
  81.             info.AddValue(p.Name, value, value.GetType());
  82.         }
  83.     }