开发者

How to save state of checkbox, textbox and ... after closing a program in c#

开发者 https://www.devze.com 2023-02-28 03:27 出处:网络
In my C# program that is made with Visual Studio 2010 and uses WinForms, I would like the program to save state of some checkboxes and textboxes so the next time program will be loaded they are checke

In my C# program that is made with Visual Studio 2010 and uses WinForms, I would like the program to save state of some checkboxes and textboxes so the next time program will be loaded they are checked or unchecked as theire last run's state. Also same with strings inside textboxes and etc...

What will be the proper way to achieve this? Is there a built in stuff in .NET? Any tips and code snippets would be app开发者_高级运维riciated!

Thanks


You'd probably want to look at reading the relevant values from your UI during the FormClosing event, and then saving them into User Settings.

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


I would bind the value to user settings, and saving the configuration OnClose event.


One way to do this is using an XML configuration file and serializing 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 have 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)
}


You asked very broad question. there are two ways to look at it.

1) If you have a need to persist application level configuration, your best bet is to use Application Settings. One can serialize program settings the user has done using your app, and restore them after the program has restarted. This works with WinForms and WPF:

2) If you need user level persistence, you need user settings.

Also, you can create custom class that implements that stores all of the configuration properties that you need.

Implement ISerializable and mark it [Serializable]. You could just mark it [Serializable], but if you add new properties in the future, you'll run into deserialization problems.

Add a Version property.

Add two static methods: Load and Save. These methods use IsolatedStorage to deserialize/serialize your configuration class to disk. You can use any kind of serialization you want - I use binary. Why not XML? Because binary is faster and users never need to get into these files. I used to do this for .net 2.0.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号