andrew4582

Settings for Winform App

Aug 27th, 2010
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.ComponentModel;
  7.  
  8. namespace ProductivityWatcher.Properties {
  9.     using Core;
  10.     using Core.Xml;
  11.     using System.Diagnostics;
  12.     using System.IO;
  13.  
  14.     [Serializable]
  15.     public class Settings {
  16.  
  17.         #region Load & Save
  18.         const string SETTINGS_FILENAME = "Settings.xml";
  19.  
  20.         static readonly Settings _default;
  21.  
  22.         public static Settings Default {
  23.             get {
  24.                 return _default;
  25.             }
  26.         }
  27.         static Settings() {
  28.             _default = new Settings();
  29.             _default.Load();
  30.         }
  31.         static string GetSettingsFilePath() {
  32.             FileInfo procInfo = new FileInfo(Process.GetCurrentProcess().MainModule.FileName);
  33.             string procDirectory = procInfo.Directory.FullName;
  34.             string settingsPath = Path.Combine(procDirectory,SETTINGS_FILENAME);
  35.             return settingsPath;
  36.         }
  37.         string GetDefaultImageDirectory() {
  38.             FileInfo finfo = new FileInfo(Process.GetCurrentProcess().MainModule.FileName);
  39.             return Path.Combine(finfo.Directory.FullName,"DesktopImages");
  40.         }
  41.         public void Reload() {
  42.             Load();
  43.         }
  44.  
  45.         public void Load() {
  46.  
  47.             string settingsPath = GetSettingsFilePath();
  48.  
  49.             string contents = null;
  50.             if(File.Exists(settingsPath))
  51.                 contents = File.ReadAllText(settingsPath);
  52.  
  53.             if(!contents.IsNullOrEmptyTrim()) {
  54.                 Settings loaded = XmlSerialization.Deserialize<Settings>(contents);
  55.                 if(loaded == null) {
  56.                     Trace.TraceWarning("Settings->Load() Failed XmlSerialization->Deserialize() returned null");
  57.                 }
  58.                 else
  59.                     TypeUtilities.CopyProperties(loaded,this);
  60.             }
  61.             else {
  62.                 CaptureIntravalInSeconds = Services.PWService.DEFAULT_CAPTURE_INTRAVAL_SEC;
  63.                 IsCaptureServiceEnabled = true;
  64.                 ImagesSaveDirectory = GetDefaultImageDirectory();
  65.                 DirectoryCreationFrequency = DirectoryCreationFrequencyType.Daily;
  66.             }
  67.         }
  68.  
  69.         public void Save() {
  70.  
  71.             string settingsPath = GetSettingsFilePath();
  72.  
  73.             if(File.Exists(settingsPath)) {
  74.                 FileAttributes att = File.GetAttributes(settingsPath);
  75.                 if(att == FileAttributes.ReadOnly) {
  76.                     File.SetAttributes(settingsPath,att | FileAttributes.Normal);
  77.                 }
  78.                 File.Delete(settingsPath);
  79.             }
  80.  
  81.             string xmldata = XmlSerialization.Serialize<Settings>(this);
  82.  
  83.             File.WriteAllText(settingsPath,xmldata);
  84.  
  85.         }
  86.         #endregion
  87.        
  88.         [DisplayName("Is Capture Service Enabled")]
  89.         [Category("Productivity Watcher")]
  90.         [Description("Gets or Sets to enable or disable the Windows Service")]
  91.         public bool IsCaptureServiceEnabled { get; set; }
  92.  
  93.         [DisplayName("Capture Intraval in Seconds")]
  94.         [Category("Productivity Watcher")]
  95.         [Description("Gets or Sets the interval in seconds to capture the windows desktop")]
  96.         public int CaptureIntravalInSeconds { get; set; }
  97.  
  98.         [DisplayName("Images Save Directory")]
  99.         [Category("Productivity Watcher")]
  100.         [Description("Gets or Sets the directory to save captured images")]
  101.         public string ImagesSaveDirectory { get; set; }
  102.  
  103.         [DisplayName("Directory Creation Frequency")]
  104.         [Category("Productivity Watcher")]
  105.         [Description("Gets or Sets the frequency of how to create directories based on time")]
  106.         public DirectoryCreationFrequencyType DirectoryCreationFrequency { get; set; }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment