by
0
8
4,302
2
Top 1% !
Popular
Famous
Tagged
Easy-to-find
Specified
Refined
OpenSource
Popularity: 2360th place

Published on:

.NETFramework4.0
Architecturex86_64
ClassNameConfigReader
Extension.cs
InputName0rootPath
InputType0string
LanguageCSharp
LicenseMIT_X11
MethodNameInitialize
OSWindows
OutputTypebool

A generic purpose Configuration Reader for .NET apps and web sites in C#

This class can be extended to provide the capability to read the content of your configuration file, e.g. the web.config in web sites, the app.config in applications as well as a custom file for other purposes
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/25613/A-generic-purpose-Configuration-Reader-f?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
using System; using System.Configuration; using System.Web; using System.IO; using System.Xml.Linq; using System.Xml; using System.Collections; using System.Collections.Generic; using log4net; using log4net.Config; public abstract class ConfigReader { #region PROTECTED PARAMETERS ///////////////////////////////////////////////////////////////////////////////////////////////////// protected string m_lastErrorMessage = string.Empty; protected XDocument m_customConfig = null; //custom config XML file protected string m_name = string.Empty; //just an example of a string property to be read protected bool m_logWarnings = false; //just an example of a boolean property to be read protected string m_rootLocalPath; #endregion ///////////////////////////////////////////////////////////////////////////////////////////////////// #region PUBLIC PROPERTIES ///////////////////////////////////////////////////////////////////////////////////////////////////// public string LastError { get { return m_lastErrorMessage; } } public string Name { get { return m_name; } } public bool LogWarnings { get { return m_logWarnings; } } #endregion ///////////////////////////////////////////////////////////////////////////////////////////////////// #region Constructors protected ConfigReader() { } public virtual bool Initialize(string rootPath) { m_rootLocalPath = rootPath; //Load custom config file if it is available: LoadCustomConfigXMLFile(); GetSetting("Server", ref m_name); if ( !GetSetting("LogWarning", ref m_logWarnings) ) m_logWarnings = false; // Log4Net initialization: FileInfo log4netFile = new FileInfo(string.Format("{0}\\log4net.config", rootPath)); if (log4netFile.Exists) XmlConfigurator.Configure(log4netFile); else XmlConfigurator.Configure(); ILog log = log4net.LogManager.GetLogger(this.GetType()); log.Info("Log4net correctly started"); return true; } #endregion ///////////////////////////////////////////////////////////////////////////////////////////////////// #region Public API public string GetSetting(string propName) { try { if (ConfigurationManager.AppSettings.Get(propName) != null) return ConfigurationManager.AppSettings.Get(propName).ToString(); else if (m_customConfig != null) { string res = string.Empty; m_customConfig.Root.ParseNode(propName, ref res); return res; } else return string.Empty; } catch { } return string.Empty; } protected bool GetSetting(string propName, ref string property) { try { if (ConfigurationManager.AppSettings.Get(propName) != null) property = ConfigurationManager.AppSettings.Get(propName).ToString(); else if (m_customConfig != null) m_customConfig.Root.ParseNode(propName, ref property); else return false; return true; } catch { return false; } } protected bool GetSetting(string propName, ref bool property) { string prop = string.Empty; if ( GetSetting(propName, ref prop) ) return bool.TryParse(prop, out property); else return false; } protected bool GetSetting(string propName, ref int property) { string prop = string.Empty; if (GetSetting(propName, ref prop)) return int.TryParse(prop, out property); else return false; } protected bool GetSetting<T>(string propName, ref T property) { string prop = string.Empty; if (GetSetting(propName, ref prop)) { try { property = (T)Enum.Parse(typeof(T), prop, true); } catch { return false; } return true; } else return false; } #endregion ///////////////////////////////////////////////////////////////////////////////////////////////////// #region Protected internal utility functions /// <summary> /// Load in memory the xml describing the configuration, if any /// </summary> /// ------------------------------------------------------------------------------------------ protected bool LoadCustomConfigXMLFile() { string path = string.Format("{0}\\mycfg.config", m_rootLocalPath); try { m_customConfig = XDocument.Load(path); } catch { // error loading document: Not found m_customConfig = null; return false; } return true; } #endregion ///////////////////////////////////////////////////////////////////////////////////////////////////// }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus