Advertisement
Guest User

Untitled

a guest
Feb 1st, 2012
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1.   public partial class ConfigComponent : UserControl
  2.   {
  3.     DisplayConfiguration displayConfig;
  4.     string configPath = "display.cfg";
  5.     public ConfigComponent()
  6.     {
  7.       InitializeComponent();
  8.       // Load Configuration
  9.       displayConfig = new DisplayConfiguration();
  10.       try {
  11.     load_config();
  12.       }
  13.       catch (Exception ex)
  14.       {
  15.         string exMessage = ex.Message;
  16.         if (ex.InnerException != null)
  17.           exMessage += " - InnerException: " + ex.InnerException.Message;
  18.         MessageBox.Show(exMessage);
  19.       }
  20.     }
  21.  
  22.     // Load Configuration
  23.     private void load_config()
  24.     {
  25.       // If no config present, create new
  26.       if (!File.Exists(configPath))
  27.       {
  28.         try
  29.         {
  30.           save_config();
  31.         }
  32.         catch (Exception)
  33.         {
  34.           throw;
  35.         }
  36.         return;
  37.       }
  38.  
  39.       using (TextReader textread = new StreamReader(configPath))
  40.       {
  41.         XmlSerializer deserializer = new XmlSerializer(typeof(DisplayConfiguration));
  42.         displayConfig = (DisplayConfiguration)deserializer.Deserialize(textread);
  43.       }
  44.  
  45.       cbx_feedactive.Checked = displayConfig.IsFeedActive;
  46.       cbx_streamactive.Checked = displayConfig.IsStreamActive;
  47.       rdo_streamlocal.Checked = displayConfig.IsStreamLocal;
  48.       txt_streamsource.Text = displayConfig.StreamSource;
  49.     }
  50.  
  51.     private void save_config()
  52.     {
  53.       // Wir speichern die Werte aus den ControlElements im Config-Objekt
  54.       displayConfig.IsFeedActive = cbx_feedactive.Checked;
  55.       displayConfig.IsStreamActive = cbx_streamactive.Checked;
  56.       displayConfig.IsStreamLocal = rdo_streamlocal.Checked;
  57.       displayConfig.StreamSource = txt_streamsource.Text;
  58.  
  59.       using (TextWriter textwriter = new StreamWriter(configPath))
  60.       {
  61.         XmlSerializer serializer = new XmlSerializer(typeof(DisplayConfiguration));
  62.         serializer.Serialize(textwriter, displayConfig);
  63.       }
  64.     }
  65.   }
  66.  
  67.   // Class that is ment to be serialized
  68.   /*
  69.    * After Hours of debugging we went a little bit apeshit here and transformed the automatic
  70.    * properties to manual properties and added XmlAttribute-Attributes. We were desperate.
  71.    * Implementing ISerializable is also one of the desperate acts - NullReferenceException persists.
  72.    */
  73.   [Serializable()]
  74.   public class DisplayConfiguration : ISerializable
  75.   {
  76.     // Klassen Properties:
  77.    
  78.     // Dumm... aber Notwendig?
  79.     private bool _IsFeedActive;
  80.     private bool _IsStreamActive;
  81.     private bool _IsStreamLocal;
  82.     private string _StreamSource;
  83.  
  84.     [XmlAttribute("IsFeedActive")]
  85.     public bool IsFeedActive {
  86.       get {return _IsFeedActive;}
  87.       set { _IsFeedActive = value; }
  88.     }
  89.    
  90.     [XmlAttribute("IsStreamActive")]
  91.     public bool IsStreamActive {
  92.       get {return _IsStreamActive;}
  93.       set { _IsStreamActive = value; }
  94.       }
  95.  
  96.     [XmlAttribute("IsStreamLocal")]
  97.     public bool IsStreamLocal
  98.     {
  99.       get {return _IsStreamLocal;}
  100.       set { _IsStreamLocal = value; }
  101.     }
  102.  
  103.     [XmlAttribute("StreamSource")]
  104.     public string StreamSource
  105.     {
  106.       get {return StreamSource;}
  107.       set { _StreamSource = value; }
  108.     }
  109.  
  110.     void ISerializable.GetObjectData(SerializationInfo oInfo, StreamingContext oContext)
  111.     {
  112.       oInfo.AddValue("IsFeedActive", this.IsFeedActive);
  113.       oInfo.AddValue("IsStreamActive", this.IsStreamActive);
  114.       oInfo.AddValue("IsStreamLocal", this.IsStreamLocal);
  115.       oInfo.AddValue("StreamSource", this.StreamSource);
  116.     }
  117.   }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement