Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi i have the following codes in App.Config, how can i set the value in it to empty every time i close the program after i have run it. FIY, this program will be published and deployed. Therefore users can only change the value in App.Config and not the program codes.

C#
<add key="EndDate" value=""/>  


Users may come to App.Config and change the EndDate every time they run the program but after they close it, the value in EndDate will set to empty automatically.

I tried this but didnt work:

C#
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(StartDate);
config.AppSettings.Settings.Add(StartDate, "");
config.AppSettings.Settings.Remove(EndDate);
config.AppSettings.Settings.Add(EndDate, "");
config.Save(ConfigurationSaveMode.Modified);
Posted
Updated 5-Jun-14 17:04pm
v2
Comments
DamithSL 5-Jun-14 22:49pm    
update the question with the code you tried
Sergey Alexandrovich Kryukov 5-Jun-14 23:10pm    
I think the problem is clear enough: it is about mixing up configuration and settings files and the idea to misuse the config file.
I provided some more reasonable advice in my answer, please see.
—SA

Bad idea. This is not what "app.config" is designed for. Only the user is supposed to modify the file (which actually takes the name after the build: "youApplicationName.exe.config"). The idea is: you put some data you need to have by default to app.config. On build the file "youApplicationName.exe.config" is created and can be deployed with the application. (Normally, your application should work correctly even if this file is absent.)

The user can have this file and use it as a starting point and edit to change some configuration detail. The application is not supposed to modify it.

If, by some reason, you really want some file with some options modifiable by the application (not matter in what phase of the runtime), you can use a setting file. I would prefer some custom XML file built by Data Contract which will directly serialize/deserialize some data classes representing some options/configuration. This file should be written to one of the locations known as "special folders" defined per user account of for "all users". For detail, please see my past answer:
How to find my programs directory[^].

See also: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

—SA
 
Share this answer
 
Comments
Jamie888 5-Jun-14 23:16pm    
does it mean that i have to remove the <add key="EndDate" value=""> and add it dynamatically each time the program is run?
check below link
C# - app config doesn't change[^]
 
Share this answer
 
Comments
Jamie888 5-Jun-14 23:35pm    
i have tried it but the result is still the same

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900