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

4

7

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 appriciated!

Thanks

Typhoeus answered 20/4, 2011 at 14:5 Comment(0)
C
7

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/

Chlorobenzene answered 20/4, 2011 at 14:7 Comment(0)
C
5

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

Cheree answered 20/4, 2011 at 14:8 Comment(0)
U
2

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)
}
Unoccupied answered 20/4, 2011 at 14:20 Comment(0)
C
1

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.

Commandeer answered 20/4, 2011 at 14:25 Comment(5)
I add new properties to my configuration class all the time and do not have any deserialization problems.Unoccupied
Thanks, does these use setting done whith Application Settings will be saved on the computer where the program is installed each time?Typhoeus
@Sean87: Asp.net does this with SettingBase class and another terminology is ASP.NET Profiler. msdn.microsoft.com/en-us/library/2y3fs9xs.aspx. It stores that where application is as well as in Databse with custom provider such as SqlProfileProvider: msdn.microsoft.com/en-us/library/0580x1f5.aspxCommandeer
Well what I am doing is just desktop program not ASP, so what would be it like in my case?Typhoeus
same machine where executable file is and SqlProfileProvider is common for windows or web application. it's simple in your case.Commandeer

© 2022 - 2024 — McMap. All rights reserved.