ConfigurationSettings.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // System.Configuration.ConfigurationSettings.cs
  3. //
  4. // Author:
  5. // Christopher Podurgiel ([email protected])
  6. //
  7. // C) Christopher Podurgiel
  8. //
  9. using System;
  10. using System.Collections.Specialized;
  11. using System.Reflection;
  12. using System.Xml;
  13. using System.Xml.XPath;
  14. using System.Windows.Forms;
  15. namespace System.Configuration
  16. {
  17. /// <summary>
  18. /// Component class.
  19. /// </summary>
  20. /// <remarks>
  21. /// Longer description
  22. /// </remarks>
  23. public sealed class ConfigurationSettings
  24. {
  25. private static string applicationConfigFileName;
  26. /// <summary>
  27. /// ConfigurationSettings Constructor.
  28. /// </summary>
  29. public ConfigurationSettings ()
  30. {
  31. }
  32. /// <summary>
  33. /// Returns configuration settings for a user-defined configuration section.
  34. /// </summary>
  35. /// <param name="sectionName">The name of the configuration section that configuration settings are read from.</param>
  36. /// <returns></returns>
  37. public static object GetConfig(string sectionName)
  38. {
  39. //Create an instance of an XML Document.
  40. XmlDocument ConfigurationDocument = new XmlDocument();
  41. /*
  42. * LAMESPEC: The .config file that needs to be parsed is the name of the application, plus ".config"
  43. * ie. "Myapplication.exe.config"
  44. * The only way I could find to get the name of the application is through System.Forms.Application.ExecutablePath, this
  45. * may be an incorrect way to get this information. It works properly on a windows machine when building an executable,
  46. * however, I'm not sure how this would work under other platforms.
  47. */
  48. //Get the full path to the Applicaton Configuration File.
  49. applicationConfigFileName = Application.ExecutablePath + ".config";
  50. //Try to load the XML Document.
  51. try
  52. {
  53. ConfigurationDocument.Load(applicationConfigFileName);
  54. }
  55. catch(XmlException e)
  56. {
  57. //Error loading the XML Document. Throw a ConfigurationException.
  58. throw(new ConfigurationException(e.Message, applicationConfigFileName, e.LineNumber));
  59. }
  60. string sectionHandlerName = GetSectionHanderType(ConfigurationDocument, sectionName);
  61. XmlNode sectionNode = ConfigurationDocument.SelectSingleNode("/configuration/" + sectionName);
  62. //If the specified sectionName is not found, then sectionNode will be null. When calling objNVSHandler.Create(),
  63. //sectionNode cannot be null.
  64. if(sectionNode == null)
  65. {
  66. return null;
  67. }
  68. //Create a new SectionHandler
  69. //According to the Docs provided by Microsoft, the user can create their own configuration sections, and create a custom
  70. //handler class for it. The user would specify the class and its assebly in the <configSections> section. These would be
  71. //seperated by a comma.
  72. string sectionHandlerClassName = sectionHandlerName;
  73. string sectionHandlerAssemblyName = "System";
  74. //Split the SectionHandler Class Name from the Assembly Name (if provided).
  75. string[] sectionHandlerArray = sectionHandlerName.Split(new char[]{','}, 2);
  76. if(sectionHandlerArray.Length == 2)
  77. {
  78. sectionHandlerClassName = sectionHandlerArray[0];
  79. sectionHandlerAssemblyName = sectionHandlerArray[1];
  80. }
  81. // Load the assembly to use.
  82. Assembly assem = Assembly.Load(sectionHandlerAssemblyName);
  83. //Get the class type.
  84. Type handlerObjectType = assem.GetType(sectionHandlerClassName);
  85. //Get a reference to the method "Create"
  86. MethodInfo createMethod = handlerObjectType.GetMethod("Create");
  87. //Create an Instance of this SectionHandler.
  88. Object objSectionHandler = Activator.CreateInstance(handlerObjectType);
  89. //define the arguments to be passed to the "Create" Method.
  90. Object[] args = new Object[3];
  91. args[0] = null;
  92. args[1] = null;
  93. args[2] = sectionNode;
  94. object sectionHandlerCollection = createMethod.Invoke(objSectionHandler, args);
  95. //Return the collection
  96. return sectionHandlerCollection;
  97. }
  98. /// <summary>
  99. /// Gets the name of the SectionHander Class that will handle this section.
  100. /// </summary>
  101. /// <param name="xmlDoc">An xml Configuration Document.</param>
  102. /// <param name="sectionName">The name of the configuration section that configuration settings are read from.</param>
  103. /// <returns>The name of the Handler Object for this configuration section, including the name if its Assembly.</returns>
  104. private static string GetSectionHanderType(XmlDocument xmlDoc, string sectionName)
  105. {
  106. //TODO: This method does not account for sectionGroups yet.
  107. string handlerName = null;
  108. //<appSettings> is a predefined configuration section. It does not have a definition
  109. // in the <configSections> section, and will always be handled by the NameValueSectionHandler.
  110. if(sectionName == "appSettings")
  111. {
  112. handlerName = "System.Configuration.NameValueSectionHandler";
  113. }
  114. else
  115. {
  116. string[] sectionPathArray = sectionName.Split(new char[]{'/'});
  117. //Build an XPath statement.
  118. string xpathStatement = "/configuration/configSections";
  119. for (int i=0; i < sectionPathArray.Length; i++)
  120. {
  121. if(i < sectionPathArray.Length - 1)
  122. {
  123. xpathStatement = xpathStatement + "/sectionGroup[@name='" + sectionPathArray[i] + "']";
  124. }
  125. else
  126. {
  127. xpathStatement = xpathStatement + "/section[@name='" + sectionPathArray[i] + "']";
  128. }
  129. }
  130. //Get all of the <section> node using the xpath statement.
  131. XmlNode sectionNode = xmlDoc.SelectSingleNode(xpathStatement);
  132. // if this section isn't found, then there was something wrong with the config document,
  133. // or the sectionName didn't have a proper definition.
  134. if(sectionNode == null)
  135. {
  136. throw (new ConfigurationException("Unrecognized element."));
  137. }
  138. handlerName = sectionNode.Attributes["type"].Value;
  139. }
  140. //Return the name of the handler.
  141. return handlerName;
  142. }
  143. /// <summary>
  144. /// Get the Application Configuration Settings.
  145. /// </summary>
  146. public static NameValueCollection AppSettings
  147. {
  148. get
  149. {
  150. //Get the Configuration Settings for the "appSettings" section.
  151. NameValueCollection appSettings = (NameValueCollection)GetConfig("appSettings");;
  152. return appSettings;
  153. }
  154. }
  155. }
  156. }