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, samples, codes and tools for .Net, SQL Server, Vista, etc.

Tuba



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:

<appSettings>

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

</appSettings>

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

AppSettings.Item("BestWebSiteOnEarth.Joke")

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

<configSections>

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

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

</configSections>

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:

<ApplicationConfiguration>

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

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

</ApplicationConfiguration>

you should add the following code to your ApplicationConfiguration  file:

Private Shared fieldCountryIdApplication As String

Public Shared ReadOnly Property ApplicationCountyId() As String

Get

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

Try

Dim setting As Object = settings(key)

If setting Is Nothing Then

ReadSetting = defaultValue

Else

ReadSetting = CStr(setting)

End If

Catch

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

Try

Dim baseHandler As NameValueSectionHandler

baseHandler = New NameValueSectionHandler

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

Catch

End Try

fieldCountryIdApplication = ReadSetting(settings, COUNTRYID_APPLICATION, COUNTRYID_APPLICATION_DEFAULT)

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

Comments

No Comments
Anonymous comments are disabled
This Blog



Add to Google

Syndication
Copyright © 2004 - 2014 Eralper Yilmaz. All rights reserved.
Community Server by Telligent Systems