0

Below is the code i am using to update or change the values in appsetting in app.config

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);              
 config.AppSettings.Settings["userName"].Value = username;
 config.AppSettings.Settings["pwd"].Value = pwd;
 config.Save(ConfigurationSaveMode.Modified, true);                
 ConfigurationManager.RefreshSection("appSettings");

i am using above code to change or update the settings in appsetting section at runtime and want the changes to persist so that when i run the application it should pick the new values from appsettings but here it doesn't happen so the changes made and saved at run time do not persist when i relaunch my application again it has the old default settings. Also i checked app.config in bin/debug but it too had the old values in appsettings. i refered various blogs and post here too as a reference but it got the same code as above but it did not persist the settings.have referred this post

Community
  • 1
  • 1
Vjendra Gaorh
  • 318
  • 2
  • 15
  • Where you are checking for the new values? In code or app.config? – Ramesh Durai Apr 18 '13 at 11:58
  • in App.config, when user gives a new username and password my application should overwrite the default username and password set in appsettings in aspp.config and next time when i launch i want the last updated username and password therefore i am looking out for a way so that i can persist any changes done to values of appsettings in app.config. but somehow above code does not save new values in appsettings. – Vjendra Gaorh Apr 18 '13 at 13:07

3 Answers3

0

I had the same problem while ago. I would have preferred to put this in a comment but I don't have that privilege. My answer might not be your case but I think is worth to be shared.

May I ask you where your bin folder is located? Windows 7 when you programmatically alter a file that isn't in a user accessible space creates a copy of that file in a Roaming space and there the file will stay. Every time you try to access the file (like your app.config) W7 transparently redirect your readings/writings to this file, so there's a chance that you are modifying the file in the roaming space, leaving the one you are lookin unaltered.

Are the changes you are making still there the successive time you start the application?

Disclaimer/Apology: I'm not an experienced user so if I am saying silly things let me know and I will remove this comment.

misleadingTitle
  • 657
  • 6
  • 21
  • sorry but your reply is not relevant to my question. about bin folder it is located inside the project folder by default when you create & build wpf project – Vjendra Gaorh Apr 18 '13 at 13:03
  • Maybe I've used bad phrasing and make some imprecise assumption but @Ramesh in the other answer has more or less said what I've said. Can you check if the next run your application recover the correct data from the app.config? – misleadingTitle Apr 18 '13 at 13:17
  • Me again. Check [this link](http://www.interworks.com/blogs/dsmith/2011/09/21/disabling-windows-7-virtual-store) in order to disable the W7 virtual store which is the security policy that I believe is causing your problem. I've tried your same code and analysing the executable with procmon I've seen that he access the file in the virtual store. – misleadingTitle Apr 18 '13 at 13:43
  • No dude it's not the case here i am not creating any new files or saving on the disk. i am just modifying the app settings in app.config and trying to save those new settings so that my application can use it next time i launch my application. Thanks for the reply though – Vjendra Gaorh Apr 18 '13 at 14:10
0

See below(from MSDN) and remember app.config is in your project. .exe.config is the actual file name. Client applications use a global configuration that applies to all users, separate configurations that apply to individual users, and configurations that apply to roaming users. The userLevel parameter determines the location of the configuration file being opened by indicating whether it has no user level (the configuration file is in the same directory as the application) or has a per-user level (the configuration file is in an application settings path determined by the user level).

Specify which configuration to get by passing one of the following values for userLevel:

To get the Configuration object that applies to all users, set userLevel to None.

To get the local Configuration object that applies to the current user, set userLevel to PerUserRoamingAndLocal.

To get the roaming Configuration object that applies to the current user, set userLevel to PerUserRoaming.
NoteNote

To get the Configuration object for a resource, your code must have read permissions on all the configuration files from which it inherits settings. To update a configuration file, your code must additionally have write permissions for both the configuration file and the directory in which it exists.
Steven Licht
  • 760
  • 4
  • 5
0

i got my solution of above problem, my goal was to persist changes done at run time at application or user level. Initially i tried using App.config where i kept default settings for application in appsettings section of app.config, but later after research i got to refer i got to know appsetting does not persist the changes, instead you can use userSettings section where under YourApplication.Property.Settings you can give your userlevel settings and it worked for me. To do this you do not need to go to App.config to do it manually, rather you can do it from the property window of project.

Right Click on your project -> Select Settings Tab on the left-> Now on the right hand side you will see the Resource section , give the ResourceName, Type, Scope and its value and you are done. The same value can be access and change dynamically from Code as well. Below are Code Excerpt for the same --

Accessing Settings Value

enter code here
        userName = Properties.Settings.Default.UserName;
        pwd = Properties.Settings.Default.PWD;

Saving New Settings Back

enter code here
        Properties.Settings.Default.UserName = userName.ToString();
        Properties.Settings.Default.PWD = newPWD..ToString();
        Properties.Settings.Default.Save();

And when you will launch your application next time you will get the new changed settings as your default settings. I hope that helps Thanks Guys

VJ

Vjendra Gaorh
  • 318
  • 2
  • 15