SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Kodyaz Development Resources
Development resources, articles, tutorials, code samples and tools and downloads for ASP.Net, SQL Server, R Script, Windows, Windows Phone, AWS, SAP HANA and ABAP, like SAP UI5, Screen Personas, etc.


working with App.config file

There are different ways to access the key values in your app.config file.

The easiest is using the appsetting section.

Here is what this section looks like in your app.config file:


<add key="BestWebSiteOnEarth.Joke" value=http://www.kodyaz.com/>


By using the System.Configuration.ConfigurationSettings namespace in your forms, you can simply acces the key value by typing :


There can be another section in your app.config file to seperate key blocks.


<section name="ApplicationConfiguration" type="SystemFramework.ApplicationConfiguration, SystemFramework"/>

<section name="ExceptionManagement" type="SystemFramework.ExceptionManagement.ExceptionManagerSectionHandler,SystemFramework"/>


The code in you forms to access those values are different. You also should have the specified classes in your project as well (ApplicationConfiguration and ExceptionManagerSectionHandler)

Those classes can be automatically prepared by using some tools and you can also manually create them. As a simple example, if you have the following key in your app.config file:


<add key="SystemFramework.Application.EnvironmentMode" value="1"/>

<add key="SystemFramework.Application.CountyId" value="1"/>


you should add the following code to your ApplicationConfiguration  file:

Private Shared fieldCountryIdApplication As String

Public Shared ReadOnly Property ApplicationCountyId() As String


ApplicationCountyId = fieldCountryIdApplication

End Get

End Property


Public Overloads Shared Function ReadSetting(ByVal settings As NameValueCollection, ByVal key As String, ByVal defaultValue As String) As String


Dim setting As Object = settings(key)

If setting Is Nothing Then

ReadSetting = defaultValue


ReadSetting = CStr(setting)

End If


ReadSetting = defaultValue

End Try

End Function


Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal section As System.Xml.XmlNode) As Object Implements IConfigurationSectionHandler.Create

Dim settings As NameValueCollection


Dim baseHandler As NameValueSectionHandler

baseHandler = New NameValueSectionHandler

settings = CType(baseHandler.Create(parent, configContext, section), NameValueCollection)


End Try


End Function

Etc, etc, I think you get the idea.

So in your other forms from where you want to access the key values you can use the following lines:

Dim config As ApplicationConfiguration

config = CType(System.Configuration.ConfigurationSettings.GetConfig("ApplicationConfiguration"), ApplicationConfiguration)

config.ApplicationCountyId.ToString() is the value you have been trying to reach for the last 5 minutes.

Enjoy coding

Published Friday, July 07, 2006 1:00 PM


No Comments
Anonymous comments are disabled
This Blog
Copyright © 2004 - 2020 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems