appSettings in config file


T

Tony Johansson

Hi!

I have this app config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="greeting" value="hello"/>
<add key="hitCount" value="0"/>
</appSettings>
</configuration>

In main below is a fully working program that reads the key hitCount and
update it's value in the app config file.

Now I wonder if there exist other possibilityto do the same I mean to read
the key hitCount and update it's value by using the object
appSettingsSection(see Main below) instead of using mySection object as I do
in the working program in Main ?

static void Main(string[] args)
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection appSettingsSection =
config.Sections["appSettings"];
AppSettingsSection mySection = config.AppSettings;
int count = Int32.Parse(mySection.Settings["hitCount"].Value);
count++;
mySection.Settings["hitCount"].Value = count.ToString();
config.Save();
Console.WriteLine("Hit count: {0}",
mySection.Settings["hitCount"].Value);
}
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top