c# application setting not saved - c#

Using VS2010 Express, Win7-64.
In my application I Have several Application settings.
Most (mixed types) are User Settings, one is Application Setting (a DataSet).
I use the built-in Settings table where the one setting scope is set as Application, while the others scope is User.
The User settings are all saved and retrieved OK, and can also be seen in the XML file.
(string userConfig = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;)
Problem: While the User settings are saved OK, the one Application Setting is not saved and is not present in the relevant config file.
(string appConfig = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None).FilePath;).
In the Settings.Design file we can see the Application Setting as read-only item (get only), which is OK, but no data is saved, and no data is found, (except a null DataSet).
There is similar DataSet in the User settings that is saved OK, and can be loaded.
To save the settings, I run:
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.Save();
Thanks

Application-scope settings are read only, and can only be changed at design time.
so when you call save method it does not persist the data.
User-scope settings, however, can be written at run time, just as you would change any property value.
for more information you can see Using Settings in C#.

Related

Not overwriting saved xml files in C# Window Form

I am fairly new to C# and I was wondering how to keep an XML file from being overwritten if one already exists on the install. In the application, there are two files that contain info to connect to the Database. One of them is relatively dynamic, but the other is saved at the setup. If I do a publish and try to update the application it always overwrites both files. Any thoughts?
You can check if the file exists with File.Exists(Path)
You can look here for more information:
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.exists?view=netcore-3.1
If this does not work you can try to read that file and if there is any data there just sont delete it.
I assume you are refering to configuration settings.
While designing your settings, in the designer, set scope to "User". This will bind the setting to the users local app settings, and will not be overwritten.
More info here: https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-create-a-new-setting-at-design-time

WPF Properties.Settings saving and multiple users

In my applications there are some setting saved in the Properties.Settings.Default. These settings can be changed by the user(s) and needs to be saved locally on the computer. While I can save these setting, the problem is that it is only saved for the user currently logged in. Once an user changes a setting it has to be for all users of the computer. How can I accomplish this?
User scoped settings are just that, settings that an individual user can change and will only be saved for that user.
The application scoped settings will affect all users but they are not designed to be changed by a user.
You might want to consider a different approach to storing settings that you want users to be able to change but to affect all users of an application e.g. the Windows registry or an external xml file.
Another option is to use user scoped settings but to change the location to a centralised location so that all users use/save the same settings. See Store user settings into application folder for an option on how to do this.
When you open the Settings Designer window in Visual Studio, you have four values that you need to enter for each setting:
You need to set the Scope property to Application to have a setting that is the same for all users. For the full story, read the Using Application Settings and User Settings page on MSDN.
Application settings cannot be changed, only by hand before the application is run so I do not recommend that approach.
In my opinion, propagating the changes may be generally a bad approach. Since this config (user.config) is generally stored in the user's own folder (under Users), it should not be modified by another user (in fact, without administrator acces, another user cannot even access).
I might recommend using other places to store application specific settings: xml or config file near your application, or maybe the registry.
I would use an external Database for that Stuff...
But if you want it quick and simple just save it to a File on the Harddrive (for example C:\Program_Data\\settings.csv) i would use a csv file because it's not much work...

How can I save form settings?

I was just wondering if anyone has any input on how to save a C# Winform setting?
Currently, I have a form that has various radio buttons, directory browsers, date pickers etc. I was wondering what is the best strategy to save these settings to an external file that can be loaded at a later date. So essentially each configuration can be loaded, executed, and then another configuration loaded. Also, the configuration can be passed across installations / users.
Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method. These settings are saved in the User.config file.
Write and Persist User Settings at Run Time
Access the user setting and assign it a new value, as shown in the following example:
Properties.Settings.Default.myColor = Color.UserGreen;
If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:
Properties.Settings.Default.Save();
I solved this problem with a class or struct which contains all settings. My form-class had a constructor which accepted such a setting-instance.
This settings-class/-struct was implementing ISerializable. So you can save it easily into files and load it from.
This is by far not the best way to do it, but it is quiet easy to implement.

Is appname.exe.config required for user specific settings?

I'm building a simple winforms application which is using built-in settings for saving program options. All of the settings are per-user.
As I can see from the generated cs file the default settings are embedded in the source code but the application still reads the appname.exe.config file when launched. If I delete the file the application seems to be working fine but I want to make sure that it will not break anything.
So is the file required if I am only using per-user settings?
You should be using user settings instead of a configuration file. Refer to this article for more information, including how to read/write user settings.
The settings designer embeds the default value in a [DefaultSettingValue] attribute. The following section in the Remarks section of the attribute is relevant:
Different settings providers may have
different requirements or limitations
on the use of the
DefaultSettingValueAttribute. For
example, the LocalFileSettingsProvider
does not require this attribute, and
will override any value provided by
this attribute if there are any
values—default or user-modified—
already present in the data store.
LocalFileSettingsProvider is the default provider. So, it's there but it doesn't get used.
If you are using the Application Settings and you want persistence of the settings then yes this is where the options are saved. If you delete the file it will simply recreate it if the user changes a setting and you are setting the setting.

How can I store user-tweakable configuration in app.config?

I know it is a good idea to store configuration data in app.config (e.g. database connection strings) instead of hardcoing it, even if I am writing an application just for myself. But is there a way to update the configuration data stored in app.config from the program that is using it?
If you use the Settings for the project, you can mark each setting as either application or user.
If they're set as user, they will be stored per-user and when you call the Save method it will be updated in the config for that user.
Code project has a really detailed article on saving all types of settings.
app.config isn't what you want to use for user-tweakable data, as it'll be stored somewhere in Program Files (which the user shouldn't have write permissions to). Instead, settings marked with a UserScopedSettingAttribute will end up in a user-scoped .config file somewhere in %LocalAppData%.
I found the best way to learn this stuff was to mess with the Visual Studio "Settings" tab (on your project's property pages), then look at the code that it generates and look in %LocalAppData% to see the file that it generates.

Categories