andrew4582

Settings Class For Windows App

Aug 27th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4.  
  5. namespace Utility
  6. {
  7.     [Serializable]
  8.     public class Settings
  9.     {
  10.         #region Static -> Load & Save Settings
  11.  
  12.         static readonly Settings _default;
  13.  
  14.         public static Settings Default
  15.         {
  16.             get
  17.             {
  18.                 return _default;
  19.             }
  20.         }
  21.  
  22.         static Settings()
  23.         {
  24.             _default = Load();
  25.         }
  26.  
  27.         public static Settings Load()
  28.         {
  29.             Settings loaded = null;
  30.  
  31.             string settingsPath = GetSettingsFilePath();
  32.             string contents = null;
  33.             if (File.Exists(settingsPath))
  34.                 contents = File.ReadAllText(settingsPath);
  35.  
  36.             if (!string.IsNullOrWhiteSpace(contents))
  37.             {
  38.                 loaded = XmlSerialization.Deserialize<Settings>(contents);
  39.                 if (loaded == null)
  40.                     Trace.TraceWarning("Settings->Load() Failed XmlSerialization->Deserialize() returned null");
  41.             }
  42.  
  43.             if (loaded == null)
  44.                 loaded = new Settings();
  45.             return loaded;
  46.         }
  47.  
  48.         public static void Save()
  49.         {
  50.             Save(Settings.Default);
  51.         }
  52.  
  53.         public static void Save(Settings data)
  54.         {
  55.             string settingsPath = GetSettingsFilePath();
  56.  
  57.             if (File.Exists(settingsPath))
  58.             {
  59.                 FileAttributes att = File.GetAttributes(settingsPath);
  60.                 if (att == FileAttributes.ReadOnly)
  61.                     File.SetAttributes(settingsPath, att | FileAttributes.Normal);
  62.                 File.Delete(settingsPath);
  63.             }
  64.  
  65.             string xmldata = XmlSerialization.Serialize<Settings>(data);
  66.  
  67.             File.WriteAllText(settingsPath, xmldata);
  68.         }
  69.  
  70.         public static string GetSettingsFilePath()
  71.         {
  72.             string settingsFileName = Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName) + ".settings.xml";
  73.             FileInfo procInfo = new FileInfo(Process.GetCurrentProcess().MainModule.FileName);
  74.             string procDirectory = procInfo.Directory.FullName;
  75.             string settingsPath = Path.Combine(procDirectory, settingsFileName);
  76.             return settingsPath;
  77.         }
  78.  
  79.         #region XmlSerialization Helper Class
  80.         /// <summary>
  81.         /// Utility class to serialize and deserialize objects
  82.         /// </summary>
  83.         [DebuggerStepThrough]
  84.         static class XmlSerialization
  85.         {
  86.             public static string Serialize<T>(T xmlObject)
  87.             {
  88.                 return Serialize<T>(xmlObject, false);
  89.             }
  90.  
  91.             public static string Serialize<T>(T xmlObject, bool encodeInBase64)
  92.             {
  93.                 using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
  94.                 {
  95.                     System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
  96.                     System.Xml.Serialization.XmlSerializerNamespaces xmlNamespace = new System.Xml.Serialization.XmlSerializerNamespaces();
  97.                     xmlNamespace.Add(string.Empty, string.Empty);
  98.                     xs.Serialize(memoryStream, xmlObject, xmlNamespace);
  99.                     System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
  100.                     if (encodeInBase64)
  101.                         return Convert.ToBase64String(System.Text.ASCIIEncoding.Unicode.GetBytes(encoding.GetString(memoryStream.ToArray())));
  102.                     else
  103.                         return encoding.GetString(memoryStream.ToArray());
  104.                 }
  105.             }
  106.  
  107.             public static string Serialize(object xmlObject)
  108.             {
  109.                 return Serialize(xmlObject, false);
  110.             }
  111.  
  112.             public static string Serialize(object xmlObject, bool encodeInBase64)
  113.             {
  114.                 using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
  115.                 {
  116.                     System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(xmlObject.GetType());
  117.                     System.Xml.Serialization.XmlSerializerNamespaces xmlNamespace = new System.Xml.Serialization.XmlSerializerNamespaces();
  118.                     xmlNamespace.Add(string.Empty, string.Empty);
  119.                     xs.Serialize(memoryStream, xmlObject, xmlNamespace);
  120.                     System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
  121.                     if (encodeInBase64)
  122.                         return Convert.ToBase64String(System.Text.ASCIIEncoding.Unicode.GetBytes(encoding.GetString(memoryStream.ToArray())));
  123.                     else
  124.                         return encoding.GetString(memoryStream.ToArray());
  125.                 }
  126.             }
  127.  
  128.             public static T Deserialize<T>(string xml)
  129.             {
  130.                 return Deserialize<T>(xml, false);
  131.             }
  132.  
  133.             public static T Deserialize<T>(string xml, bool decodeInBase64)
  134.             {
  135.                 string data = xml;
  136.                 if (decodeInBase64)
  137.                     data = System.Text.ASCIIEncoding.Unicode.GetString(Convert.FromBase64String(xml));
  138.                 System.Xml.Serialization.XmlSerializer xs = System.Xml.Serialization.XmlSerializer.FromTypes(new Type[] { typeof(T) })[0];
  139.                 System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
  140.                 using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(encoding.GetBytes(data)))
  141.                 {
  142.                     System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(memoryStream, System.Text.Encoding.UTF8);
  143.                     return (T)xs.Deserialize(memoryStream);
  144.                 }
  145.             }
  146.         }
  147.  
  148.         #endregion
  149.  
  150.         #endregion
  151.  
  152.         public Settings()
  153.         {
  154.             //set defaults here
  155.         }
  156.  
  157.         //[DisplayName("Images Save Directory")]
  158.         //[Category("Productivity Watcher")]
  159.         //[Description("Gets or Sets the directory to save captured images")]
  160.         public int LastAppID { get; set; }
  161.         public int LastDivisionID { get; set; }
  162.         public string LastDefaultObjectKey { get; set; }
  163.         public int LastDepartmentNumber { get; set; }
  164.         public int LastClassificationID { get; set; }
  165.         public int LastDocTypeID { get; set; }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment