How to save flag state, text box and ... after closing the program in C #

In my C # program created using Visual Studio 2010 and using WinForms, I would like the program to save the state of some flags and text fields, so that the next program to load will be loaded, checked or unchecked as the last launch state. Same thing with lines inside text fields, etc.

What would be the right way to achieve this? Is there any embedded material in .NET? Any tips and code snippets will be published!

thanks

+7
source share
4 answers

You will probably want to familiarize yourself with the corresponding values ​​from the user interface during the FormClosing event and then save them in the user settings.

Take a look: http://codehill.com/2009/01/saving-user-and-application-settings-in-winforms/

+7
source

I would bind the value to user preferences and save the configuration of the OnClose event.

+5
source

One way to do this is to use the XML configuration file and serialize it:

ConfigManager.cs

using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace MyApplication { [ Serializable() ] public class ConfigManager { private int removeDays = 7; public ConfigManager() { } public int RemoveDays { get { return removeDays; } set { removeDays = value; } } } 

somewhere in your application

private ConfigManager cm;

private XmlSerializer ser;

...

Then you need to load the configuration:

 private void LoadConfig() { try { cm = new ConfigManager(); ser = new XmlSerializer(typeof(ConfigManager)); filepath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + cm.filepath; if (File.Exists(filepath)) { FileStream fs = new FileStream(filepath, FileMode.Open); cm = (ConfigManager)ser.Deserialize(fs); // do something } } catch (Exception ex) { } } 

To save it:

 XmlSerializer ser; ConfigManager cm; ... private void saveConfig() { try { cm.RemoveDays = 6; TextWriter tw = new StreamWriter(filepath, false); ser.Serialize(tw, cm); tw.Close(); } catch (Exception ex) } 
+2
source

You asked a very broad question. There are two ways to look at this.

1) If you need to save the application level configuration, it is best to use Application Settings . You can serialize the program parameters that the user has completed using your application and restore them after the program is restarted. This works with WinForms and WPF:

2) If you require user level constancy, you need user settings .

In addition, you can create a custom class that implements that stores all the necessary configuration properties.

Implement ISerializable and check it [Serializable]. You can simply mark it [Serializable], but if you add new properties in the future, you will run into problems of deserialization.

Add Version property.

Add two static methods: Load and save. These methods use IsolStorage to deserialize / serialize your configuration class to disk. You can use whatever serialization you want - I use a binary. Why not XML? Because the binary is faster and users should never get into these files. I used this for .net 2.0.

+1
source

All Articles