public void LoadSettings() { try { // Create an instance of the Settings class Settings settings = new Settings(); if (File.Exists(this.settingsPath)) { // Create an instance of System.Xml.Serialization.XmlSerializer XmlSerializer serializer = new XmlSerializer(typeof(Settings)); // Create an instance of System.IO.StreamReader // to point to the settings file on disk StreamReader textReader = new StreamReader(this.settingsPath); // Create an instance of System.Xml.XmlTextReader // to read from the StreamReader XmlTextReader xmlReader = new XmlTextReader(textReader); if (serializer.CanDeserialize(xmlReader)) { // Assign the deserialized object to the new settings object settings = ((Settings)serializer.Deserialize(xmlReader)); this.allowedShapes = settings.AllowedShapes; this.drawSpeed = settings.DrawSpeed; this.customSpeed = settings.CustomSpeed; this.useTransparency = settings.UseTransparency; } else { // Save a new settings file this.SaveSettings(); } // Close the XmlTextReader xmlReader.Close(); // Close the XmlTextReader textReader.Close(); } else { // Save a new settings file this.SaveSettings(); } } catch (Exception ex) { MessageBox.Show(string.Format("Error retrieving deserialized settings! {0}", ex.Message), "Dave on C# Screen Saver", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void ScreenSaverForm_Load(object sender, EventArgs e) { try { // Initialise private members this.random = new Random(); this.settings = new Settings(); this.settings.LoadSettings(); this.active = false; // Hide the cursor Cursor.Hide(); // Create the Graphics object to use when drawing. this.graphics = this.CreateGraphics(); // Set the draw speed from the settings file switch (this.settings.DrawSpeed) { case Settings.Speed.Custom: tmrMain.Interval = this.settings.CustomSpeed; break; case Settings.Speed.Fast: tmrMain.Interval = 100; break; case Settings.Speed.Normal: tmrMain.Interval = 200; break; case Settings.Speed.Slow: tmrMain.Interval = 500; break; } // Enable the timer. tmrMain.Enabled = true; } catch (Exception ex) { MessageBox.Show(string.Format("Error loading screen saver! {0}", ex.Message), "Dave on C# Screen Saver", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void SaveSettings() { try { // Create an instance of the Settings class Settings settings = new Settings(); settings.AllowedShapes = this.allowedShapes; settings.DrawSpeed = this.drawSpeed; if (settings.DrawSpeed == Settings.Speed.Custom) settings.CustomSpeed = this.customSpeed; else settings.CustomSpeed = 0; settings.UseTransparency = this.useTransparency; // Create an instance of System.Xml.Serialization.XmlSerializer XmlSerializer serializer = new XmlSerializer(settings.GetType()); // Create an instance of System.IO.TextWriter // to save the serialized object to disk TextWriter textWriter = new StreamWriter(this.settingsPath); // Serialize the settings object serializer.Serialize(textWriter, settings); // Close the TextWriter textWriter.Close(); } catch (Exception ex) { MessageBox.Show(string.Format("Error saving serialized settings! {0}", ex.Message), "Dave on C# Screen Saver", MessageBoxButtons.OK, MessageBoxIcon.Error); } }