How can I save form settings? - c#

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.

Related

Save user settings for different users in WinForms?

I want to have a save preference option in my WinForms application. The user chooses yes/no and has an option to save preference, upon which the yes/no will be remembered and a form for such a choice will not be popped upon further re running the application.
I read about going to setting and changing but how to do it for different users, since all of them would choose for diff options and I need to maintain that.
Simply using a boolean variable will not help since it will be single user specific. Any suggestions?
(1) At event close main form, you call method/action save result. See How to save data when closing event happen in WinForms?
You can save it to XML file (or text file, SQL lite, SQL Server, etc), popular way is XML file(s).
Read XML file to get saved result before processing business logic in your WinForms application at Load form event or another event type at early stage of application start-up period.
(2) You also save result at previous period in config file https://stackoverflow.com/a/453230/3728901
Properties.Settings.Default["SomeProperty"] = "Some Value";
Properties.Settings.Default.Save(); // Saves settings in application configuration file
So since i didn't want a database , I am creating a file at the client side and saving the preference there.
At run time , I will read from the file and upon that , will decide whether to send form or not .
you can use a Model and store use selected config into it then you should serilaize it and save on database for specific user when reRun the application you can get config and deserialize it to Model and use it

c# application setting not saved

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#.

writing User configuration back to app.config in .net application

I have a windows application. I am writing all user specific settings into user.config.
Now in a new scenario, whatever changes one user does it should reflect to all the users.
For that purpose I thought of writing the settings into app.config through
Properties.Settings.Default.p1 = value;
Properties.Settings.Default.Save();
And every user read the updated default value after
Reset();
But what ever I do…it still writes to user.config not to app.config
From my experience, the Settings class cannot be used to modify default setting values for all users. In this situation, I modify the app.config directly using the classes in the System.Xml namespace. It is not ideal, but I have not found any other way to do it.
There are a couple things to keep in mind when modifying your app.config file:
If your program is installed in the Program Files folder, you may need to run your application as an administrator in order to save changes to the file. It will depend on how your security is configured.
If you change the namespace of your Settings class, it will change the structure of your app.config file, so you would need to update your code to account for the new XML structure.
See this MSDN article for more information about application and user scoped settings:
http://msdn.microsoft.com/en-us/library/8eyb2ct1%28v=vs.110%29.aspx

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...

Preserve data between application executions

I have an application with some textboxes. My user fills the textboxes and runs some methods, when they close the application data is lost (normally).
I want to keep the value of a couple of textboxes and some local variables. It's not worth it to use database, and simple .txt files are not clean enough, is there any other simple and brief way of storing little volumes of data between application runs?
I'm not sure but have heard some wisps about resource files, are they good for this case?
Simplest way is binding your textboxes to application settings:
select texbox you want to preserve
go to Properties > Data > (ApplicationSettings)
add application settings binding to Text property
on FormClosed event save application settings
Saving settings:
private void Form_FormClosed(object sender, FormClosedEventArgs e)
{
Settings.Default.Save();
}
Next time when user will start your application, settings will be loaded from user-specific file, and textboxes will be filled with same data as it was before user closed an application last time.
Also in application settings you can store local variables, but you will have to add settings for them manually, and manually read that setting on application start:
open Properties folder under project > Settings.settings
add settings you want to store (e.g. MyCounter)
set MyCounter type, scope, and default value (e.g. int, User, 0)
read setting to your local variable var x = Settings.Default.MyCounter
on form closed save setting Settings.Default.MyCounter = x just before calling Settings.Default.Save()
There are a couple of options, but with most of them, you're going to be putting a file somewhere, whether it's a text file, resources/config or binary.
Using settings is one option: http://www.codeproject.com/Articles/17659/How-To-Use-the-Settings-Class-in-C
You can also take the serialization route: http://msdn.microsoft.com/en-us/library/vstudio/et91as27.aspx
Or you could possibly look into noSQL databases like MongoDB: http://www.mongodb.org/
You have the following options
A local Microsoft Access database which can store small footprint.
Use a Dictionary, Serialize / Deserialize to filesystem.
The Windows registry.
Assuming you're on Windows (as the tags imply), have you considered the registry?

Categories