Advertisement
Epoc

ConfigurationManager.cs

Sep 15th, 2015
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Xml;
  6.  
  7.  
  8.     /// <summary>
  9.  
  10.     /// Provides access to configuration files for client applications on the .NET Compact Framework.
  11.  
  12.     /// </summary>
  13.  
  14.     public static class ConfigurationManager
  15.     {
  16.  
  17.         #region Private Members
  18.  
  19.  
  20.  
  21.         private static NameValueCollection appSettings = new NameValueCollection();
  22.  
  23.         private static string configFile;
  24.  
  25.  
  26.  
  27.         #endregion
  28.  
  29.  
  30.  
  31.         #region Public Properties
  32.  
  33.  
  34.  
  35.         /// <summary>
  36.  
  37.         /// Gets configuration settings in the appSettings section.
  38.  
  39.         /// </summary>
  40.  
  41.         public static NameValueCollection AppSettings
  42.         {
  43.  
  44.             get
  45.             {
  46.  
  47.                 return appSettings;
  48.  
  49.             }
  50.  
  51.         }
  52.  
  53.  
  54.  
  55.         #endregion
  56.  
  57.  
  58.  
  59.         #region Constructors
  60.  
  61.  
  62.  
  63.         /// <summary>
  64.  
  65.         /// Static constructor.
  66.  
  67.         /// </summary>
  68.  
  69.         static ConfigurationManager()
  70.         {
  71.  
  72.             // Determine the location of the config file
  73.  
  74.             ConfigurationManager.configFile = String.Format("{0}.config", System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
  75.  
  76.  
  77.  
  78.             // Ensure configuration file exists
  79.  
  80.             if (!File.Exists(ConfigurationManager.configFile))
  81.             {
  82.  
  83.                 throw new FileNotFoundException(String.Format("Configuration file ({0}) could not be found.", ConfigurationManager.configFile));
  84.  
  85.             }
  86.  
  87.  
  88.  
  89.             // Load config file as an XmlDocument
  90.  
  91.             XmlDocument myXmlDocument = new XmlDocument();
  92.  
  93.             myXmlDocument.Load(ConfigurationManager.configFile);
  94.  
  95.  
  96.  
  97.             // Add keys and values to the AppSettings NameValueCollection
  98.  
  99.             foreach (XmlNode appSettingNode in myXmlDocument.SelectNodes("/configuration/appSettings/add"))
  100.             {
  101.  
  102.                 ConfigurationManager.AppSettings.Add(appSettingNode.Attributes["key"].Value, appSettingNode.Attributes["value"].Value);
  103.  
  104.             }
  105.  
  106.         }
  107.  
  108.  
  109.  
  110.         #endregion
  111.  
  112.  
  113.  
  114.         #region Public Methods
  115.  
  116.  
  117.  
  118.         /// <summary>
  119.  
  120.         /// Saves changes made to the configuration settings.
  121.  
  122.         /// </summary>
  123.  
  124.         public static void Save()
  125.         {
  126.  
  127.             // Load config file as an XmlDocument
  128.  
  129.             XmlDocument myXmlDocument = new XmlDocument();
  130.  
  131.             myXmlDocument.Load(ConfigurationManager.configFile);
  132.  
  133.  
  134.  
  135.             // Get the appSettings node
  136.  
  137.             XmlNode appSettingsNode = myXmlDocument.SelectSingleNode("/configuration/appSettings");
  138.  
  139.  
  140.  
  141.             if (appSettingsNode != null)
  142.             {
  143.  
  144.                 // Remove all previous appSetting nodes
  145.  
  146.                 appSettingsNode.RemoveAll();
  147.  
  148.  
  149.  
  150.                 foreach (string key in AppSettings.AllKeys)
  151.                 {
  152.  
  153.                     // Create a new appSetting node
  154.  
  155.                     XmlElement appSettingNode = myXmlDocument.CreateElement("add");
  156.  
  157.  
  158.  
  159.                     // Create the key attribute and assign its value
  160.  
  161.                     XmlAttribute keyAttribute = myXmlDocument.CreateAttribute("key");
  162.  
  163.                     keyAttribute.Value = key;
  164.  
  165.  
  166.  
  167.                     // Create the value attribute and assign its value
  168.  
  169.                     XmlAttribute valueAttribute = myXmlDocument.CreateAttribute("value");
  170.  
  171.                     valueAttribute.Value = AppSettings[key];
  172.  
  173.  
  174.  
  175.                     // Append the key and value attribute to the appSetting node
  176.  
  177.                     appSettingNode.Attributes.Append(keyAttribute);
  178.  
  179.                     appSettingNode.Attributes.Append(valueAttribute);
  180.  
  181.  
  182.  
  183.                     // Append the appSetting node to the appSettings node
  184.  
  185.                     appSettingsNode.AppendChild(appSettingNode);
  186.  
  187.                 }
  188.  
  189.             }
  190.  
  191.  
  192.  
  193.             // Save config file
  194.  
  195.             myXmlDocument.Save(ConfigurationManager.configFile);
  196.  
  197.         }
  198.  
  199.  
  200.  
  201.         #endregion
  202.  
  203.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement