Solved

C# Reading and Saving static values into config file values Configuration Manager

Posted on 2011-03-21
2
419 Views
Last Modified: 2012-05-11
I have a console application that I have to modify to create and use a counter that would be incremented with every application's run regardless who is using the application.
The counter's value has to be unique, that's why I cannot use user settings, so it should be independent of a user.
The application was created by VS 2010.  The application uses the Properties.Settings file that is also using similar counter, but it is on the User level.
I learned that the value of the counter from that file will be saved into the user.config file so each user would have different user.config file and different values to be saved.  It is incorrect for the purpose of this application.
The AppSettings values are read only in the Property.Settings file, so I cannot use them as well.
 
I want to use the application's config file instead, and the ConfigurationManager class to read ans write the counter.  (I don't want to use the ConfigurationSettings class because it is said to be deprecated.)

But I am having issues using the application.config file that already exists in the application, but if I open it, it simply duplicate the Property.Setting file that I cannot use.

How should I save and modify the counter?
Samples will be much appreciated.
I wa
0
Comment
Question by:pavelmed
2 Comments
 
LVL 5

Accepted Solution

by:
morgulo earned 500 total points
ID: 35185038
Try this:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

int cnt = int.Parse(config.AppSettings.Settings["cnt"].Value);
cnt++;
config.AppSettings.Settings["cnt"].Value = cnt.ToString();     
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

Open in new window

0
 

Author Comment

by:pavelmed
ID: 35186207
Thank you for your answer.  It worked!

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question