473,775 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

app.config

I'm working in VS2003.NET, with C#.

I use app.config to store the connection string to my SQL Server. This
works fine.

Lately I've been taking work home where the connection string is
different. I deal with this by having app.config.home and
app.config.work files which I copy over the app.config file as required
by my location.

I was wondering if there is another way to do this. In my Microsoft.NET
file system hierarchy I notice a CONFIG directory with a machine.config
file in it. Said machine.config file has an example appSettings section
in it.

Would that be an appropriate place to store my SQL Server connection
string? How would I access it? I currently say
'ConfigurationS ettings.AppSett ings["strCon"]'. How would that change?

Thank you,

-- Rick
Jul 21 '05 #1
4 20244
Rick,
I would recommend a single app.config file. Within this single app.config
have custom sections that identify distinct 'environments' (home or work).

There is a predefined configSections section in the app.config that you use
to define new sections.

Something like:

<configuratio n>

<configSections >
<section name="myClass1S tuff"
type="System.Co nfiguration.Dic tionarySectionH andler, System" />
<sectionGroup name="environme nts">
<section name="work"
type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
<section name="home"
type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
</sectionGroup>
</configSections>

<appSettings>
<add key="key1" value="value1" />
<add key="environmen t" value="work" />
</appSettings>

<myClass1Stuf f>
<add key="key1" value="value1" />
</myClass1Stuff>

<environments >
<work value1="xyz" value2="abc" value3="edf" value4="ghi" />
<home value1="xyz" value2="abc" value3="edf" value4="ghi" />
</environments>

</configuration>

If you inherit from DictionarySecti onHandler you can easily change the key
or value in your section. I've derived from DictionarySecti onHandler to
change key/value to plugin/type in a few of my projects.

I use the above environments section to define each of my environments,
Production, QA, Test, Development. Where each environment points to the
correct database servers, web servers, message queues, printers any 'per
environment' settings. The appSettings/environment setting is used to
identify the current environment in use. Instead of value1, value2, value3,
value4. I would have connectionStrin g, printer, messageQueue and such that
are distinct per environment.

You then need to use System.Configur ation.Configura tionSettings.Ge tConfig to
get your section, which returns an object based on the type of section
handler defined for that section (usually HashTable, but can be other object
types.

See the following on how to create new sections via the configSections
section.

http://msdn.microsoft.com/library/de...onhandlers.asp

and:
http://msdn.microsoft.com/library/de...ionsschema.asp

Also read about the System.Configur ation.Configura tionSettings class and
other classes in the System.Configur ation namespace.

If you define a distinct enough custom section you could put it in the
machine config, however I would put it under something other than
appSettings, as all applications can read appSettings (technically All .NET
applications can read all sections of machine config). If I were to put a
custom section in machine.config I would more than likely use the namespace
for my assembly as the custom section name. For the above I would define a
custom section to hold which environment I was using, then

Hope this helps
Jay
"Guinness Mann" <GM***@dublin.c om> wrote in message
news:MP******** *************** @news.newsguy.c om...
I'm working in VS2003.NET, with C#.

I use app.config to store the connection string to my SQL Server. This
works fine.

Lately I've been taking work home where the connection string is
different. I deal with this by having app.config.home and
app.config.work files which I copy over the app.config file as required
by my location.

I was wondering if there is another way to do this. In my Microsoft.NET
file system hierarchy I notice a CONFIG directory with a machine.config
file in it. Said machine.config file has an example appSettings section
in it.

Would that be an appropriate place to store my SQL Server connection
string? How would I access it? I currently say
'ConfigurationS ettings.AppSett ings["strCon"]'. How would that change?

Thank you,

-- Rick

Jul 21 '05 #2
Jay,

Perhaps I'm dense, but my objective is to be able to zip up a project,
take it home, and run it without change. As near as I can tell, this
solution just pushes the problem of what to edit to someplace else. (I
haven't figured out where, yet :-) )

You say:
all applications can read appSettings (technically All .NET
applications can read all sections of machine config).
Actually, that sounds perfect to me. At work, all applications need to
access the same SQL Server. Likewise at home. I can see where in a
more complex environment with more servers it would be a problem.

So then, when I call System.Configur ation.AppSettin s["section"] does it
search for "section" in both places: app.config and machine.config?

-- Rick
In article <eZ************ *@TK2MSFTNGP11. phx.gbl>,
Ja********@emai l.msn.com says... Rick,
I would recommend a single app.config file. Within this single app.config
have custom sections that identify distinct 'environments' (home or work).

There is a predefined configSections section in the app.config that you use
to define new sections.

Something like:

<configuratio n>

<configSections >
<section name="myClass1S tuff"
type="System.Co nfiguration.Dic tionarySectionH andler, System" />
<sectionGroup name="environme nts">
<section name="work"
type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
<section name="home"
type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
</sectionGroup>
</configSections>

<appSettings>
<add key="key1" value="value1" />
<add key="environmen t" value="work" />
</appSettings>

<myClass1Stuf f>
<add key="key1" value="value1" />
</myClass1Stuff>

<environments >
<work value1="xyz" value2="abc" value3="edf" value4="ghi" />
<home value1="xyz" value2="abc" value3="edf" value4="ghi" />
</environments>

</configuration>

If you inherit from DictionarySecti onHandler you can easily change the key
or value in your section. I've derived from DictionarySecti onHandler to
change key/value to plugin/type in a few of my projects.

I use the above environments section to define each of my environments,
Production, QA, Test, Development. Where each environment points to the
correct database servers, web servers, message queues, printers any 'per
environment' settings. The appSettings/environment setting is used to
identify the current environment in use. Instead of value1, value2, value3,
value4. I would have connectionStrin g, printer, messageQueue and such that
are distinct per environment.

Jul 21 '05 #3
Rick,
So then, when I call System.Configur ation.AppSettin s["section"] does it
search for "section" in both places: app.config and machine.config?
ConfigurationSe ttings reads machine.config first, then it "merges" what is
in app.config. I understand that it physically reads the config files once,
and caches the results returned from ConfigurationSe ttings.GetConfi g,
avoiding extra overhead of continually reading & searching the files.

This merging allows app.config to override what is in machine.config. It is
also part of the reason that every thing in appSettings is normally <add>,
it can be <remove> or <clear>

What I tried to say at the bottom of my response is if you use
machine.config DO NOT USE appSettings. Define your own custom section,
similar to the "environmen ts" section in my example, just make sure you name
the section for the namespace of your assembly. Also make sure this name
follows the .NET guide lines (company.Tehcno logyName).

http://msdn.microsoft.com/library/de...guidelines.asp

To minimize interfering with future .NET upgrades and other class libraries
that may be installed.

Hope this helps
Jay
"Guinness Mann" <GM***@dublin.c om> wrote in message
news:MP******** *************** *@news.newsguy. com... Jay,

Perhaps I'm dense, but my objective is to be able to zip up a project,
take it home, and run it without change. As near as I can tell, this
solution just pushes the problem of what to edit to someplace else. (I
haven't figured out where, yet :-) )

You say:
all applications can read appSettings (technically All .NET
applications can read all sections of machine config).


Actually, that sounds perfect to me. At work, all applications need to
access the same SQL Server. Likewise at home. I can see where in a
more complex environment with more servers it would be a problem.

So then, when I call System.Configur ation.AppSettin s["section"] does it
search for "section" in both places: app.config and machine.config?

-- Rick
In article <eZ************ *@TK2MSFTNGP11. phx.gbl>,
Ja********@emai l.msn.com says...
Rick,
I would recommend a single app.config file. Within this single app.config have custom sections that identify distinct 'environments' (home or work).
There is a predefined configSections section in the app.config that you use to define new sections.

Something like:

<configuratio n>

<configSections >
<section name="myClass1S tuff"
type="System.Co nfiguration.Dic tionarySectionH andler, System" />
<sectionGroup name="environme nts">
<section name="work"
type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
<section name="home"
type="System.Co nfiguration.Sin gleTagSectionHa ndler, System" />
</sectionGroup>
</configSections>

<appSettings>
<add key="key1" value="value1" />
<add key="environmen t" value="work" />
</appSettings>

<myClass1Stuf f>
<add key="key1" value="value1" />
</myClass1Stuff>

<environments >
<work value1="xyz" value2="abc" value3="edf" value4="ghi" />
<home value1="xyz" value2="abc" value3="edf" value4="ghi" />
</environments>

</configuration>

If you inherit from DictionarySecti onHandler you can easily change the key or value in your section. I've derived from DictionarySecti onHandler to
change key/value to plugin/type in a few of my projects.

I use the above environments section to define each of my environments,
Production, QA, Test, Development. Where each environment points to the
correct database servers, web servers, message queues, printers any 'per
environment' settings. The appSettings/environment setting is used to
identify the current environment in use. Instead of value1, value2, value3, value4. I would have connectionStrin g, printer, messageQueue and such that are distinct per environment.

Jul 21 '05 #4
[This followup was posted to microsoft.publi c.dotnet.genera l and a copy
was sent to the cited author.]

In article <Of************ **@TK2MSFTNGP10 .phx.gbl>,
Ja********@emai l.msn.com says...
ConfigurationSe ttings reads machine.config first, then it "merges" what is
in app.config. I understand that it physically reads the config files once,
and caches the results returned from ConfigurationSe ttings.GetConfi g,
avoiding extra overhead of continually reading & searching the files.
...
Hope this helps


Thanks, Jay. That was just right!

-- Rick

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3021
by: Maxim Khesin | last post by:
I want to have a config file with my python proggie, satisfying the following requirements: 1) support key->(value, default) 2) simple and intuitive to read and edit 3) easyly readable into a python datastructure (a dictionary?) 4) not requiring any heavy libraries needed (I am distributing my proggie as a py2exe executable and do not want to bloat the size) can you guys suggest some format for this? thanks, max
4
3828
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply* access config files. This is version 3, which is a big overhaul. It extends ConfigObj to reading config files with sections and various other simplifications. I find ConfigObj extremely easy to use and use it for reading config files and data...
3
3820
by: Richard Lewis Haggard | last post by:
I have a test application that is calling an assembly that reads some strings out of a config file. Normally, this assembly supports a web application and the information can be read just fine. Then I created a C# test application and ran the function. The function fails because the configuration read is failing to find a key. I created an application config and copied the keys over to the app config file but the app is still failing to...
13
507
by: Khodr | last post by:
Hello, I am using VS.NET 2003 and vb. I build my application MyApp and it generates MyApp.exe.config. So now MyApp.exe reads parameters from MyApp.exe.config. Great and no problem! I need to run the same program but with different configuration data. So I made a copy of MyApp.exe and MyApp.exe.config to put them in another folder and renamed the copy to MyApp2.exe and MyApp2.exe.config respectively. I ran it but it did not read from...
20
2624
by: tomerfiliba | last post by:
hey i've been seeing lots of config-file-readers for python. be it ConfigObj (http://www.voidspace.org.uk/python/configobj.html) or the like. seems like a trend to me. i came to this conclusion a long time ago: YOU DON'T NEED CONFIG FILES FOR PYTHON. why re-invent stuff and parse text by yourself, why the interpreter can do it for you? and anyway, i find this a very ugly format:...
11
3451
by: TARUN | last post by:
Hello All I need to ask about the configuration file in .NET, There are Two config File 1. Web Config 2. Machine config I understand the the usage of Web config , but not able to understand the usage of Machine config. I read in the article that you can also write your database connection string in Machine Config
12
13438
by: dbuchanan | last post by:
Hello, (Is this the proper newsgroup?) === Background === I am building a solution with two projects. One project is my data access layer which contains my DataSet as an xsd file. The XSD file was built by draging tables from the Data Sources pane. Auto-generated code created the files associated wtih the XSD file (xss,
5
7865
by: mmcd79 | last post by:
I built a VB.net application that makes use of a machine level DB connection string setting, and a user level starting location setting. The machine level setting and the default user based setting is of course stored in the app.exe.config file located in the same directory as the exe. Upon closing the form, I save the user setting which then creates a user.config file in the appdata directory in my profile. This is all well and good....
10
2060
by: eagle | last post by:
I have a web.config in my application that contains the connection strings to all my datasources. I want to move these connection strings to another web config up the folder hierarchy so that all my apps can use the same connection strings. That is supposed to be how it's done, no? Instead of the web.config being in c:\inetpub\wwwroot\myApp\web.config, I have it in c:\inetpub\wwwroot\web.config. However, I get an "Object reference not...
5
2861
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a app that uses several membership/role providers. I can list these Providers with the code: Dim rootWebConfig1 As Configuration rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath) Dim section As New MembershipSection section = rootWebConfig1.GetSection("system.web/membership")
0
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9915
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4014
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.