Preserve data between application executions - c#

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?

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

Windows form Options for local storage(no DB)

I have a windows form application. I need to store date value it should be available after the application restart. One option is saving in a file which can be accessible and modified.I have tried embedded resource but it is read only.Is there any option for windows form application to persist such variables.
You can use a local file (xml, ini, app.config, etc...) or you can use the Windows Registry if you don't want to write it in a file.
You can use Service based database(local db) just for saving that data.
At first store some default values and then update and retrieve when ever necessary.
It works for me.Not sure it's efficient!

How to retrieve a variable in memory from a WPF application?

I have a WPF application, when the app runs I need to store a variable in a shared memory.
When the app is being closed and successively restart I need to get the variable previously stored. I need a simple solution, I would avoid using a text file saved in some place.
If the machine is restarted I do now need to get that variable.
Any idea what are my option?
You could use your Settings file to save properties between application sessions.
For more information see this and note the Saving User Settings at Run Time section. It technically is "saved" to a file, but in a much dev-friendly manner.
Edit: That is a link for VS2005, oops! Use ConfigurationManager.AppSettings and refer to this post when saving for newer apps.
If you're wanting to avoid writing/reading files, you could use the Registry.

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.

c# default variables? Settings.Defaullt,defaultCity=txtcity.text; how to do them?

Well i downloaded a program for wheater but it is not mind now, i watched, than you write a city and you close the program, and when you open it again, it has de last city you write, how is it? i watched it has this code in whaterform_formClosed
but how do i create these variables? and if this is posible can I to do a program without a database? saveing all in a dataset? or datatables? default databales? default dataset?
but now i want to know how to create a Default variable
private void weatherForm_FormClosed(object sender, FormClosedEventArgs e)
{
//Save Settings
Settings.Default.defaultCity = txtCity.Text;
Settings.Default.intervalText = comboBoxEdit1.Text;
Settings.Default.windowPosition = this.Location;
Settings.Default.timerOn = timer1.Enabled;
Settings.Default.intervalTime = delay;
Settings.Default.Save();
}
You can use 'settings' to do this:
Starting with the .NET Framework 2.0,
you can create and access values that
are persisted between application
execution sessions. These values are
called settings. Settings can
represent user preferences, or
valuable information the application
needs to use. For example, you might
create a series of settings that store
user preferences for the color scheme
of an application. Or you might store
the connection string that specifies a
database that your application uses.
Settings allow you to both persist
information that is critical to the
application outside the code, and to
create profiles that store the
preferences of individual users.
See Using Application Settings and User Settings for more information.
The easiest way to store information like this is in the config file (like it is in your example)
If you open the properties of your project, it contains a tab called settings. Add the properties that you want in there, make sure they have a scope of User and you should be able to do something very similar.

Categories