// Add a Reference to System.Configuration
using System;
using System.Configuration;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Text;
namespace EE_ConfigXML
{
//
// Allows for the editing of key/value pairs in the config file. This required
// because the My.Settings values for the Application scope are read only, and
// the values for User scope are recorded elsewhere.
// Note: You can only change a value, you can not add or delete a key
//
public class ConfigXML
{
private Configuration cfg;
private XmlDocument xd_app;
private XmlDocument xd_user;
private string AssemblyName;
public enum Section : int
{
Application = 0,
User
}
//
// The default constructor loads the configuration file from the
// currently running application
//
public ConfigXML()
{
cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AssemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
LoadSections();
}
//
// Load the configuration file for some other application
//
public ConfigXML(string ConfigPath, string ExpectedAssemblyName)
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
ConfigurationSectionGroup s = default(ConfigurationSectionGroup);
if (File.Exists(ConfigPath))
{
map.ExeConfigFilename = ConfigPath;
cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
// Get the Assembly name from the config file
s = cfg.SectionGroups["applicationSettings"];
if ((s == null))
{
throw new ApplicationException("Configuration file missing, corrupt, or not the correct version");
}
AssemblyName = s.Sections[0].SectionInformation.Name.Split('.')[0];
if (AssemblyName != ExpectedAssemblyName)
{
throw new ApplicationException("Incorrect Configuration file\rExpecting configuration for " + ExpectedAssemblyName + ", however the configuration file is for " + AssemblyName);
}
}
else
{
throw new System.IO.FileNotFoundException();
}
LoadSections();
}
//
// Save the changes back to the config file
//
public void SaveSettings()
{
ConfigurationSection sec_app = default(ConfigurationSection);
ConfigurationSection sec_user = default(ConfigurationSection);
StringBuilder sb = default(StringBuilder);
XmlWriterSettings xws = default(XmlWriterSettings);
XmlWriter xw = default(XmlWriter);
// Create settings for the XmlWriter to match the formatting of
// the *.config file
xws = new XmlWriterSettings();
xws.Indent = true;
xws.IndentChars = " ";
xws.OmitXmlDeclaration = true;
// prep the XmlWriter
sb = new StringBuilder();
xw = XmlWriter.Create(sb, xws);
if ((xd_app != null))
{
xd_app.Save(xw);
// replace the entire applicationSettings section
sec_app = (ConfigurationSection)cfg.GetSection("applicationSettings/" + AssemblyName + ".Properties.Settings");
sec_app.SectionInformation.SetRawXml(sb.ToString());
}
// do it again for the userSettings section
sb = new StringBuilder();
xw = XmlWriter.Create(sb, xws);
if ((xd_user != null))
{
xd_user.Save(xw);
sec_user = (ConfigurationSection)cfg.GetSection("userSettings/" + AssemblyName + ".Properties.Settings");
sec_user.SectionInformation.SetRawXml(sb.ToString());
}
// save the changes to the config file
cfg.Save();
}
//
// Get a value from a key/value pair from the configuration file
//
public string GetValue(Section Sec, string Key)
{
XmlNode xn = default(XmlNode);
xn = null;
// Use XPath navigation to find the proper node
switch (Sec)
{
case Section.Application:
if ((xd_app == null))
{
return null;
}
xn = xd_app.SelectSingleNode("//setting[@name=\"" + Key + "\"]");
break;
case Section.User:
if ((xd_user == null))
{
return null;
}
xn = xd_user.SelectSingleNode("//setting[@name=\"" + Key + "\"]");
break;
}
if ((xn != null))
{
// is this a serialized string array?
if (xn.InnerXml.StartsWith("";
}
else
{
xn.InnerXml = "" + value + "";
}
}
}
//
// Load the two XML documents with the appropriate section
//
private void LoadSections()
{
ConfigurationSection sec_app = default(ConfigurationSection);
ConfigurationSection sec_user = default(ConfigurationSection);
// get the applicationSettings section
sec_app = (ConfigurationSection)cfg.GetSection("applicationSettings/" + AssemblyName + ".Properties.Settings");
if ((sec_app != null))
{
// make an XmlDocument out of that section
xd_app = new XmlDocument();
xd_app.LoadXml(sec_app.SectionInformation.GetRawXml());
}
// do it again for the userSettings section
sec_user = (ConfigurationSection)cfg.GetSection("userSettings/" + AssemblyName + ".Properties.Settings");
if ((sec_user != null))
{
// make an XmlDocument out of that section
xd_user = new XmlDocument();
xd_user.LoadXml(sec_user.SectionInformation.GetRawXml());
}
}
//
// Convert an XML serialized string array into a string
//
private static string XMLStringArraytoString(string buf)
{
System.Xml.Serialization.XmlSerializer xmls = default(System.Xml.Serialization.XmlSerializer);
StringReader sr = default(StringReader);
string[] temp = null;
// build a serializer that handles string arrays
xmls = new System.Xml.Serialization.XmlSerializer(typeof(string[]));
// do the deed
sr = new StringReader(buf);
temp = (string[])xmls.Deserialize(sr);
if (temp.Length == 0)
{
return "";
}
return String.Join("\r\n", temp);
}
//
// Convert a string array into an XML serialized string
//
private static string String2XMLStringArray(string buf)
{
System.Xml.Serialization.XmlSerializer xmls = default(System.Xml.Serialization.XmlSerializer);
StringBuilder sb = default(StringBuilder);
XmlWriter xw = default(XmlWriter);
XmlWriterSettings xws = default(XmlWriterSettings);
string[] temp = null;
// trim any extra blank lines at the end
buf = buf.TrimEnd("\r\n".ToCharArray());
// build a string array
temp = buf.Split("\r\n".ToCharArray());
for (int i = 0; i <= temp.Length; i++)
{
temp[i] = temp[i].Trim('\n');
}
// setup the formatting options
xws = new XmlWriterSettings();
xws.Indent = true;
xws.IndentChars = " ";
xws.OmitXmlDeclaration = true;
// prep the XmlWriter
sb = new System.Text.StringBuilder();
xw = System.Xml.XmlWriter.Create(sb, xws);
// build a serializer that handles string arrays
xmls = new System.Xml.Serialization.XmlSerializer(typeof(string[]));
// do the deed
xmls.Serialize(xw, temp);
// add a bit of padding to each line
temp = sb.ToString().Split("\r\n".ToCharArray());
for (int i = 0; i <= temp.Length; i++)
{
temp[i] = " " + temp[i].Trim('\n');
}
return String.Join("\r", temp);
}
}
}
|