/// <summary> /// Creates the configuration object from the configuration section. /// </summary> /// <param name="parent">The parent object.</param> /// <param name="configContext">The configuration context object.</param> /// <param name="section">The section as XML node.</param> /// <returns>The created section handler object.</returns> public object Create(object parent, object configContext, System.Xml.XmlNode section) { if (section == null) { throw new ArgumentNullException("section"); } XmlNode element = section.FirstChild; while (element != null && !typeof(XmlElement).IsInstanceOfType(element)) { element = element.NextSibling; } XmlNodeReader reader = new XmlNodeReader(element); try { DataContractSerializer serializer = new DataContractSerializer(typeof(ConfigurationLocation)); ConfigurationLocation configuration = serializer.ReadObject(reader) as ConfigurationLocation; return(configuration); } finally { reader.Close(); } }
/// <summary> /// Creates the configuration object from the configuration section. /// </summary> /// <param name="parent">The parent object.</param> /// <param name="configContext">The configuration context object.</param> /// <param name="section">The section as XML node.</param> /// <returns>The created section handler object.</returns> public object Create(object parent, object configContext, System.Xml.XmlNode section) { if (section == null) { throw new ArgumentNullException(nameof(section)); } XmlNode element = section.FirstChild; while (element != null && typeof(XmlElement) != element.GetType()) { element = element.NextSibling; } XmlReader reader = XmlReader.Create(new StringReader(element.OuterXml)); try { DataContractSerializer serializer = new DataContractSerializer(typeof(ConfigurationLocation)); ConfigurationLocation configuration = serializer.ReadObject(reader) as ConfigurationLocation; return(configuration); } finally { reader.Dispose(); } }
/// <summary> /// Reads the file path from the application configuration file. /// </summary> /// <param name="sectionName">Name of configuration section for the current application's default configuration containing <see cref="ConfigurationLocation"/>. /// </param> /// <returns>File path from the application configuration file.</returns> public static string GetFilePathFromAppConfig(string sectionName) { string filePath = null; #if !SILVERLIGHT ConfigurationLocation location = ConfigurationManager.GetSection(sectionName) as ConfigurationLocation; if (location != null) { filePath = location.FilePath; } if (String.IsNullOrEmpty(filePath)) { // get the default application name from the executable file. FileInfo file = new FileInfo(Environment.GetCommandLineArgs()[0]); // choose a default configuration file. filePath = Utils.Format( "{0}\\{1}.Config.xml", file.DirectoryName, file.Name.Substring(0, file.Name.Length - 4)); } // convert to absolute file path (expands environment strings). string absolutePath = Utils.GetAbsoluteFilePath(filePath, true, false, false); if (absolutePath != null) { return(absolutePath); } #endif // return the invalid file path. return(filePath); }