Complex configuration files

  • Thread starter Thread starter Richard Kerr
  • Start date Start date
R

Richard Kerr

Hi all,
Have a look at this config file structure:

<appSettings>
<add key="connstr" value="workstation id=test;packet size=4096;user
id=sa;data source=test;persist security info=False;initial
catalog=test;password=test" />
</appSettings>
<mysettings>
<add key="intErrorLogtype" value="1" />
<add key="EventLogKeyName" value="FRISBEE" />
<add key="sys_DBCheck" value="usp_CheckSQLConnection" />
<add key="strCEPath" value="" />
</mysettings>

As you can see i have added a new element called <mysetting> that will
contain additional things that I want to store outside of the standard
appsettings tree.

How, in code, do I retrieve values from the section in my app.config
file under the section <mysettings>

thanks in advance for your help on this matter

Kerr

PS this is a VB.NET windows Forms application
 
Ray,
Thanks for the speedy reply. I have a question for you. My first attempt
at doing this invovled creating configSections and then using the
following code to try and grab the value out of the app.config file.

Dim obj As IDictionary = _

CType(System.Configuration.ConfigurationSettings.GetConfig("sectionname"
),_
IDictionary)

However, this always returns nothing.

Is the implementation for configSections only available in asp.net
applications and not in windows forms?
 
No, it works fine with Windows forms as well... here is a quick example of
one of my bits of code (from memory again):

Dim cfgLogLevels As NameValueCollection

cfgLogLevels = ConfigurationSettings.GetConfig("GEM_Log/LogLevels")
m_rootLevel = CType(cfgLogLevels.Get("Root"), Integer)


Here is the sample app.config file that goes with it:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="GEM_Log">
<section name="LogLevels"
type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<GEM_Log>
<LogLevels>
<add key = "Root" value="5" />
</LogLevels>
</GEM_Log>
</configuration>

Hope this helps...
 
Ray,
Sorry for being a complete muppet but the namevaluecollection object you
have in code refers to the
system.Collections.Specialized.NameValueCollection right?

Well when i implement your code as you've defined the intellisense
throws an error saying cannot convert an namevaluecollection to an
object.

any ideas?
 
Ray,
Please ignore my last post. That is not my problem. Your code does
work.

The problem i just discovered that I am having is that my app.config is
not attached to my Application project but to a dll project (Business
Logic) which is a separate project to the actual GUI. i.e.


App.exe (is standalone project)
Bl.dll (is standalane project)
DS.dll (is standalone project)

the app.config I am trying to read from is attched to the BL.dll and
when my app tries to look for this file it only check the app.exe. Thus
why nothing is being returned.

Not sure how to fix this but will keep on.

Cheers for your help in the matter anyway.

Kerr
 
That is normal. dll projects do not use their own config file. They use the
config file of the host exe assembly.
 
Back
Top