![]() |
Desktop Development »
Files and Folders »
Configuration Files
Intermediate
A custom configuration file AppSettings reader classBy Daniel Fisher (lennybacon)This article describes how to create a custom configuration file AppSettings reader class. |
C#.NET1.0, .NET1.1, Win2K, WinXP, Win2003VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I'll explain how to build a AppSettings reader class that can be used with every .config file you want.
When you deploy .dll assemblies (especially for ASP.NET applications), you are forced to use the main application's configuration file. Some of my applications have a lot of entries to add to the AppSettings section of the .config file and it is annoying to do all the modification to the configuration file to just run the application.
To access the configuration file, the following class returns a custom IDictionary object:
public class CustomConfigurationSettings
{
public static AppSettingsReader AppSettings(string configFile)
{
return new AppSettingsReader(configFile);
}
}
You can call it in your code like this:
object settingsValue =
StaticDust.Configuration.CustomConfigurationSettings.AppSettings(
"C:\\yourFile.config")["yourKey"];
To get the file named {yourAssembly}.config in a web application, call it as follows:
Assembly assmebly = Assembly.GetExecutingAssembly();
string configFile =
System.Web.HttpContext.Current.Request.PhysicalApplicationPath +
"bin\\" + assmebly.GetName().ToString().Split(',')[0] + ".config";
object settingsValue =
StaticDust.Configuration.CustomConfigurationSettings.AppSettings(
configFile)["yourKey"];
CustomConfigurationSettings.
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 Oct 2003 Editor: Smitha Vijayan |
Copyright 2003 by Daniel Fisher (lennybacon) Everything else Copyright © CodeProject, 1999-2010 Web21 | Advertise on the Code Project |