Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I'm trying to modify my app.config file by editing values in it during runtime. I'm not able to see the changes in my app.config File. This is the function that takes a key-value pair and saves the changes in the configuration file. Please let me know if I need to change anything in this function to make it work.

C#
public void SetAppSettingsVal(string node, string newValue)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings[node].Value = newValue;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
Posted

1 solution

If you're running into this problem on Windows Vista and above, keep in mind anything under Program Files is ReadOnly. Users cannot save changes back to the app.config file under there.
 
Share this answer
 
Comments
Philippe Mori 25-Jul-11 19:47pm    
In fact, settings might have been virtualized. That is, changes that are not allowed ad silently written to the virtual store for the logged user. If you want to share data, you must explicitly allows access to all user to that data.
Dave Kreskowiak 26-Jul-11 9:57am    
Reading back the settings would also be virtualized, so his setting should not have disappeared. My reply was more of a "public service announcement" that you shouldn't writing anything to Program Files.

Redirection is not something I would be relying on either.
deepthakannan 26-Jul-11 9:30am    
My application files including App.config file are not under ProgramFiles directory.So I should be able to read and write app.config, right?

I don't know what config.Save() is doing. If it is not able to save the changes, it should throw an exception or give me some indication that it failed. What is happening there?
Dave Kreskowiak 26-Jul-11 9:58am    
Where did you put your .EXE and config files? How are you verifying that the settings are not getting saved??
deepthakannan 27-Jul-11 9:49am    
Everything is in the Output folder (debug/bin). If I open my app.config, or my project name.config file in VS2010, I don't see the changes I made using this function.

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