Reputation: 946
Using this answer: https://stackoverflow.com/a/453230/3012708 I thought i'd successfully implemented saving of settings in my C# application.
However, the saving never seems to happen - the applications config file is never updated, and if i add some code to be called from SettingChangingEventHandler or SettingsSavingEventHandler it's never called.
I added settings as desribed in the answer referenced above. 4 bool values in "User" scope, with the Access Modifier setting left at "Internal".
In my code i call:
Using MyApplication.Properties;
Then to set the setting "Interval", i do:
Settings.Default.Interval = true;
And to save:
Settings.Default.Save();
If i check the MyApplication.exe.config file, i see the setting is there, like:
<userSettings>
<MyApplication.Properties.Settings>
<setting name="Interval" serializeAs="String">
<value>False</value>
</setting>
</MyApplication.Properties.Settings>
</userSettings>
However, it still has it's original value - false.
Any ideas?
I tried without the "Using..." code, and using the full code to update:
MyApplication.Properties.Settings.Default.Interval = true;
MyApplication.Properties.Settings.Default.Save();
I also tried running the application as Admin outside of Visual Studio - no luck.
Upvotes: 1
Views: 4649
Reputation: 4339
Are you sure the settings aren't being set? note: they will be set under the users' profile, not in the solution folder. Generally in c:\Users\<username>\AppData\Local\<appname>
(AppData is a hidden folder).
Upvotes: 4
Reputation: 2282
MyApplication.exe.config is a file that is used for deployment. You're saving the settings correctly but the exe.config file contains the original values hence why you don't see any change.
If you want to check if it saved correctly just write
if(Properties.Settings.Default.Interval)
{
throw new Exception("Is True");
}
From MSDN:
These apps have two configuration files: a source configuration file, which is modified by the developer during development, and an output file that is distributed with the app.
When you develop in Visual Studio, place the source configuration file for your app in the project directory and set its Copy To Output Directory property to Copy always or Copy if newer. The name of the configuration file is the name of the app with a .config extension. For example, an app called myApp.exe should have a source configuration file called myApp.exe.config.
Visual Studio automatically copies the source configuration file to the directory where the compiled assembly is placed to create the output configuration file, which is deployed with the app. In some cases, Visual Studio may modify the output configuration file; for more information, see the Redirecting assembly versions at the app level section of the Redirecting Assembly Versions article.
Upvotes: 3