[2.0] how to read the contents of app.config file in a loop?
I want to read the all elements under <appSettings> tag in a loop.
this is my app.config file
Code:
<appSettings>
<add key="SourceFile_Advances" value="C:\Code\SavvysoftValuations\MV ADVS.csv" />
<add key="DestinationFile_Advances" value="C:\Code\SavvysoftValations\Destination\MV ADVS.csv"/>
<add key="SourceFile_aaa" value="C:\Code\SavvysoftValuations\MV SWPS.csv" />
<add key="DestinationFile_aaa" value="C:\Code\SavvysoftValations\Destination\MV SWPS.csv"/>
</appSettings>
currently I am using the following code to read the elements . but I would like to read the elements (under appsettings) in a loop . If in future if new elements are added or deleted under appSettings tag, I don't want to make any changes to my application. Basically, instead of hard coding the element name, I would like to read all the elements under <appSettings> in a loop. How can I do that ? I can load the appSettings into an XMLDocument object and traverse but I was wondering if there is any better wa.
Code:
_valuations_Advances_sourceFile = System.Configuration.ConfigurationSettings.AppSettings["SourceFile_Advances"];
_valuations_advances_destinationFile = System.Configuration.ConfigurationSettings.AppSettings["DestinationFile_Advances"];
Re: [2.0] how to read the contents of app.config file in a loop?
Re: [2.0] how to read the contents of app.config file in a loop?
AppSettings is type NameValueCollection. Use a For Each loop like you do any other collection. Also, ConfigurationSettings.AppSettings is obsolete in .NET 2.0, which I believe the IDE should have told you. You should be using ConfigurationManager.AppSettings in .NET 2.0 if I'm not mistaken.
Re: [2.0] how to read the contents of app.config file in a loop?
VB Code:
NameValueCollection nvc = ConfigurationManager.AppSettings;
foreach (string tete in nvc.AllKeys)
{
System.Console.WriteLine(nvc[tete]);
}