Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class ConfigComponent : UserControl
- {
- DisplayConfiguration displayConfig;
- string configPath = "display.cfg";
- public ConfigComponent()
- {
- InitializeComponent();
- // Load Configuration
- displayConfig = new DisplayConfiguration();
- try {
- load_config();
- }
- catch (Exception ex)
- {
- string exMessage = ex.Message;
- if (ex.InnerException != null)
- exMessage += " - InnerException: " + ex.InnerException.Message;
- MessageBox.Show(exMessage);
- }
- }
- // Load Configuration
- private void load_config()
- {
- // If no config present, create new
- if (!File.Exists(configPath))
- {
- try
- {
- save_config();
- }
- catch (Exception)
- {
- throw;
- }
- return;
- }
- using (TextReader textread = new StreamReader(configPath))
- {
- XmlSerializer deserializer = new XmlSerializer(typeof(DisplayConfiguration));
- displayConfig = (DisplayConfiguration)deserializer.Deserialize(textread);
- }
- cbx_feedactive.Checked = displayConfig.IsFeedActive;
- cbx_streamactive.Checked = displayConfig.IsStreamActive;
- rdo_streamlocal.Checked = displayConfig.IsStreamLocal;
- txt_streamsource.Text = displayConfig.StreamSource;
- }
- private void save_config()
- {
- // Wir speichern die Werte aus den ControlElements im Config-Objekt
- displayConfig.IsFeedActive = cbx_feedactive.Checked;
- displayConfig.IsStreamActive = cbx_streamactive.Checked;
- displayConfig.IsStreamLocal = rdo_streamlocal.Checked;
- displayConfig.StreamSource = txt_streamsource.Text;
- using (TextWriter textwriter = new StreamWriter(configPath))
- {
- XmlSerializer serializer = new XmlSerializer(typeof(DisplayConfiguration));
- serializer.Serialize(textwriter, displayConfig);
- }
- }
- }
- // Class that is ment to be serialized
- /*
- * After Hours of debugging we went a little bit apeshit here and transformed the automatic
- * properties to manual properties and added XmlAttribute-Attributes. We were desperate.
- * Implementing ISerializable is also one of the desperate acts - NullReferenceException persists.
- */
- [Serializable()]
- public class DisplayConfiguration : ISerializable
- {
- // Klassen Properties:
- // Dumm... aber Notwendig?
- private bool _IsFeedActive;
- private bool _IsStreamActive;
- private bool _IsStreamLocal;
- private string _StreamSource;
- [XmlAttribute("IsFeedActive")]
- public bool IsFeedActive {
- get {return _IsFeedActive;}
- set { _IsFeedActive = value; }
- }
- [XmlAttribute("IsStreamActive")]
- public bool IsStreamActive {
- get {return _IsStreamActive;}
- set { _IsStreamActive = value; }
- }
- [XmlAttribute("IsStreamLocal")]
- public bool IsStreamLocal
- {
- get {return _IsStreamLocal;}
- set { _IsStreamLocal = value; }
- }
- [XmlAttribute("StreamSource")]
- public string StreamSource
- {
- get {return StreamSource;}
- set { _StreamSource = value; }
- }
- void ISerializable.GetObjectData(SerializationInfo oInfo, StreamingContext oContext)
- {
- oInfo.AddValue("IsFeedActive", this.IsFeedActive);
- oInfo.AddValue("IsStreamActive", this.IsStreamActive);
- oInfo.AddValue("IsStreamLocal", this.IsStreamLocal);
- oInfo.AddValue("StreamSource", this.StreamSource);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement