2 // System.Configuration.ConfigurationSettings.cs
5 // Christopher Podurgiel (cpodurgiel@msn.com)
7 // C) Christopher Podurgiel
11 using System.Collections.Specialized;
12 using System.Reflection;
14 using System.Xml.XPath;
16 namespace System.Configuration
22 /// Longer description
25 public sealed class ConfigurationSettings
28 private static string applicationConfigFileName;
31 /// ConfigurationSettings Constructor.
33 public ConfigurationSettings ()
39 /// Returns configuration settings for a user-defined configuration section.
41 /// <param name="sectionName">The name of the configuration section that configuration settings are read from.</param>
42 /// <returns></returns>
43 public static object GetConfig(string sectionName)
45 //Create an instance of an XML Document.
46 XmlDocument ConfigurationDocument = new XmlDocument();
49 * LAMESPEC: The .config file that needs to be parsed is the name of the application, plus ".config"
50 * ie. "Myapplication.exe.config"
51 * The only way I could find to get the name of the application is through System.Forms.Application.ExecutablePath, this
52 * may be an incorrect way to get this information. It works properly on a windows machine when building an executable,
53 * however, I'm not sure how this would work under other platforms.
55 //Get the full path to the Applicaton Configuration File.
56 applicationConfigFileName = "FIXME:ConfigurationSettings" + ".config";
58 //Try to load the XML Document.
61 ConfigurationDocument.Load(applicationConfigFileName);
65 //Error loading the XML Document. Throw a ConfigurationException.
66 throw(new ConfigurationException(e.Message, applicationConfigFileName, e.LineNumber));
69 string sectionHandlerName = GetSectionHanderType(ConfigurationDocument, sectionName);
71 XmlNode sectionNode = ConfigurationDocument.SelectSingleNode("/configuration/" + sectionName);
75 //If the specified sectionName is not found, then sectionNode will be null. When calling objNVSHandler.Create(),
76 //sectionNode cannot be null.
77 if(sectionNode == null)
83 //Create a new SectionHandler
\r
85 //According to the Docs provided by Microsoft, the user can create their own configuration sections, and create a custom
\r
86 //handler class for it. The user would specify the class and its assebly in the <configSections> section. These would be
\r
87 //seperated by a comma.
\r
89 string sectionHandlerClassName = sectionHandlerName;
90 string sectionHandlerAssemblyName = "System";
\r
92 //Split the SectionHandler Class Name from the Assembly Name (if provided).
\r
93 string[] sectionHandlerArray = sectionHandlerName.Split(new char[]{','}, 2);
94 if(sectionHandlerArray.Length == 2)
96 sectionHandlerClassName = sectionHandlerArray[0];
97 sectionHandlerAssemblyName = sectionHandlerArray[1];
100 // Load the assembly to use.
101 Assembly assem = Assembly.Load(sectionHandlerAssemblyName);
102 //Get the class type.
103 Type handlerObjectType = assem.GetType(sectionHandlerClassName);
104 //Get a reference to the method "Create"
105 MethodInfo createMethod = handlerObjectType.GetMethod("Create");
106 //Create an Instance of this SectionHandler.
107 Object objSectionHandler = Activator.CreateInstance(handlerObjectType);
109 //define the arguments to be passed to the "Create" Method.
110 Object[] args = new Object[3];
113 args[2] = sectionNode;
115 object sectionHandlerCollection = createMethod.Invoke(objSectionHandler, args);
117 //Return the collection
118 return sectionHandlerCollection;
124 /// Gets the name of the SectionHander Class that will handle this section.
\r
126 /// <param name="xmlDoc">An xml Configuration Document.</param>
\r
127 /// <param name="sectionName">The name of the configuration section that configuration settings are read from.</param>
\r
128 /// <returns>The name of the Handler Object for this configuration section, including the name if its Assembly.</returns>
130 private static string GetSectionHanderType(XmlDocument xmlDoc, string sectionName)
132 //TODO: This method does not account for sectionGroups yet.
133 string handlerName = null;
135 //<appSettings> is a predefined configuration section. It does not have a definition
136 // in the <configSections> section, and will always be handled by the NameValueSectionHandler.
137 if(sectionName == "appSettings")
139 handlerName = "System.Configuration.NameValueSectionHandler";
144 string[] sectionPathArray = sectionName.Split(new char[]{'/'});
146 //Build an XPath statement.
147 string xpathStatement = "/configuration/configSections";
148 for (int i=0; i < sectionPathArray.Length; i++)
150 if(i < sectionPathArray.Length - 1)
152 xpathStatement = xpathStatement + "/sectionGroup[@name='" + sectionPathArray[i] + "']";
156 xpathStatement = xpathStatement + "/section[@name='" + sectionPathArray[i] + "']";
160 //Get all of the <section> node using the xpath statement.
161 XmlNode sectionNode = xmlDoc.SelectSingleNode(xpathStatement);
163 // if this section isn't found, then there was something wrong with the config document,
164 // or the sectionName didn't have a proper definition.
165 if(sectionNode == null)
168 throw (new ConfigurationException("Unrecognized element."));
171 handlerName = sectionNode.Attributes["type"].Value;
175 //Return the name of the handler.
182 /// Get the Application Configuration Settings.
184 public static NameValueCollection AppSettings
188 //Get the Configuration Settings for the "appSettings" section.
189 NameValueCollection appSettings = (NameValueCollection)GetConfig("appSettings");;