Should I be making more use of the config file?
Firstly where is it, secondly what kind of stuff should I be using it for? I havent used it for anything as it sounded like I shouldnt mess with it!
Printable View
Should I be making more use of the config file?
Firstly where is it, secondly what kind of stuff should I be using it for? I havent used it for anything as it sounded like I shouldnt mess with it!
To add a config file, right click your project > add new item > configuration file. I use them in nearly every project I use to keep away from hard coding data. Database connection strings are a good example of what to put in. You can also put dictionary objects in your config. Here is one of mine:
Code:<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ReQue" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<appSettings>
<!-- Define the Ultraedit info -->
<add key="UltraPath" value="\\sdn_apollo\hew\HEW_Common\Applications\ULTRAEDT10\"/>
<add key="UltraName" value="UEDIT32.EXE"/>
<add key="UltraINI" value="Fred.INI"/>
<add key="TempFolder" value="C:\Temp\Chase\"/>
<!-- Monitor ECN upload -->
<add key="WebServer" value="http://scs.com/files.html"/>
<!-- Define the BTS server settings -->
<add key="DefaultServer" value="Production"/>
<add key="Internal" value="\\intest\hew_*************\"/>
<add key="External" value="\\bztst\hew_*************\"/>
<add key="Production" value="\\btsp\hew_*************\"/>
<!-- Define the SQL server Settings -->
<add key="DBInternal" value="server=MSSQL\I;database=HTC;Integrated Security=SSPI"/>
<add key="DBExternal" value="server=MSSQL\E;database=HTC;Integrated Security=SSPI"/>
<add key="DBProduction" value="server=MSSQL\P;database=HTC;Integrated Security=SSPI"/>
<!-- The following server selection is used for archive and sent older than 3 months -->
<add key="Previous" value="\\Sdn\hew\HEW_Common\HEW_*************\"/>
<!-- Output directory where reports are requeued -->
<add key="OutputLocation" value="\Output\"/>
</appSettings>
<ReQue>
<!-- Keys must be unique -->
<add key="997" value="HTC\Archive"/>
<add key="TA1" value="HTC\Archive"/>
<add key="EFL" value="HTC\Archive"/>
<add key="RPT" value="HTC\Archive"/>
<add key=".997" value="Medicare_Only\Archive"/>
<add key=".TA1" value="Medicare_Only\Archive"/>
<add key=".EFL" value="Medicare_Only\Archive"/>
<add key=".RPT" value="Medicare_Only\Archive"/>
</ReQue>
</configuration>
Thanks at least I know now where to get one from.
I normally declare my connection string as a public read only property in a DataBase Class I create, what is the advantage of the config way? Alot of it doesnt make sense how are properties accessed? Is it mainly then just used for storing common properties?
If you declare a connection string in all your programs, whenever a database change occurs you'll have to edit, recompile, and deploy those programs. This is a way to do it dynamically. It also helps if you use different databases in test and production environments. You would have identical executables on booth servers, but a setting in the config file could be changed depending on how you need it.
So its a file that sits on a server and is referenced by the programs at execution time? Thus changes to the file will automatically be replicated to all reliant programs?
The config file resides in the same directory as the executable file. For example, if you called your application 'Test', then you would have a 'Test.exe.config' file located in the directory. Whenever the program starts, it checks this file for any configuration directives that it needs to pick up and applies them as necessary.
You can also reference this file from within the program (as wild_bill was talking about) by way of the ConfigurationSettings.AppSettings method. That way, you can have settings that may change constantly in an XML file that can be easily changed instead of having to re-compile just to change one setting.
Kyjan
So any change to a database connection string would still require the modified config file to be replaced on each machine on the network as the config file is stored on the client pc?
Unless you're running the program from a network share, in which case the config file is stored in that network share directory.
Kyjan
As far as I know you cannot share a config file between multiple programs.Quote:
Originally Posted by FishGuy