Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-21
2
Medium Priority
?
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
morgulo earned 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

660 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