Search the articles  
  

• Custom attributes in app.config web.config file

Tuesday, August 04, 2009

download : library files with demo codes

There are a lot of method available for creating custom attributes in your app.config or web.config configuration file but every method must implement IConfigurationSectionHandler interface.

By categorizing the different attributes in your project configuration file, it makes your project more easier to handle different group of configuration setting.

For example if you don't want to mix database configuration setting with ftp configuration setting, you can try as below.

   <!--DB Provider Type OldDB=0 MS SQL=1 Oracle=2-->
   <DBType>
      <add key="MS ACCESS" value="0"/>
      <add key="SQL Server" value="1"/>
   </DBType>
   <FTP>
      <add key="FTPIP" value="1.1.1.1"/>
      <add key="FTPPort" value="21" />
      <add key="FTPUser" value="xxx"/>
      <add key="FTPPW" value="xxx"/>
      <add key="FTPTransferType" value="1"/>
      <add key="FTPChDir" value=""/>
   </FTP>
   <appSettings>
      ....
   </appSettings>

But in order to access your own custom attributes, DBType and FTP, you need to create your own handler by implementing the IConfigurationSectionHandler interface and it's methods.

Create a AppSettingHandler.cs file and generate a dll file.

Add your AppSettingHandler into your configuration file. App.config or Web.config
Reference an assembly dll file generated with AppSettingHandler.cs.

Now, you can access your own custom attribute "DBType" with ConfigurationManager class.
The code will be as below.

Hashtable dbList = AppConfig.GetAppValueList("DBType");

AppConfig is a class that allow you to use GetAppValueList static method.


download : library files with demo codes

No comments:

Post a Comment