开发者

Restoring state of a form in C# when application starts

In my c# application i have this small form which is used to set parameters of application. Once i set the parameters my application runs fine. when i restart the application the values of field in the parametric form are reset.

how can i store and restore the contents of the form between closi开发者_运维问答ng and starting of my application


Sounds like you should be storing those configuration values in the App.config file (or one of the other configuration managers .NET provides you).

You can then build your form around serializing and maintaining those values, rather than something custom.

...no need to re-invent the wheel.


The application and user settings provided by the .NET framework are good for this:

The Application Settings feature of Windows Forms makes it easy to create, store, and maintain custom application and user preferences on the client computer. With Windows Forms application settings, you can store not only application data such as database connection strings, but also user-specific data, such as user application preferences. Using Visual Studio or custom managed code, you can create new settings, read them from and write them to disk, bind them to properties on your forms, and validate settings data prior to loading and saving.

Start with the Application Settings Overview topic at MSDN.


While there are several approaches to this, if you're dealing with "global" application settings that apply to all users of the application, you can create an app.config file and add the setting keys into the AppSettings portion of the app.config file.

The slightly tricky part is modifying those settings and having them stay, since by default simply calling build in accessors to the configuration file will not save the settings. You'll want to use the ConfigurationManager class and make sure to call the Refresh command once things are saved.

It took me a day or two to piece this altogether a while back, but I wrote a blog entry about it here.


Sounds like a perfect opportunity to read a little bit about Settings in .NET

Settings can be used to store and retrieve data between different execution sessions and there's basically two different types of settings; Application settings and user settings. Application settings is for storing application specific settings and user is for storing user preferences.


The best way I've found to store application settings, that a user is able to change, is to use the IsolatedStorageFile class. Here's an example of using Isolated Storage in a WPF application's App.xaml file. This populates the Application.Properties dictionary with user created values. The properties are then available later in the application.

public partial class App : Application
{
        string fileName = "App.txt";

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // read Iso Storage file
                        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
            try
            {
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
                using (StreamReader reader = new StreamReader(stream))
                {
                    while (!reader.EndOfStream)
                    {
                        // populate Application Properties
                                                string[] keyValue = reader.ReadLine().Split(new char[] { ',' });
                        this.Properties[keyValue[0]] = keyValue[1];
                    }
                }
            }
            catch (FileNotFoundException)
            {
                // Set default values.  You would probably want to read these values from a config file
                                this.Properties["LocalServiceAddress"] = "http://localhost/myservice";
                this.Properties["TranscodeServer"] = "http://localhost";

            }
        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            // Persist application-scope property to isolated storage
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))            
            using (StreamWriter writer = new StreamWriter(stream))
            {
                // Persist each application-scope property individually
                foreach (string key in this.Properties.Keys)
                {
                    writer.WriteLine("{0},{1}", key, this.Properties[key]);
                }
            }
        }
    }


You need to serialize them somewhere (like the registry or a file) then read them in when the application starts up.


You could serialize your parameters out to disk (in XML, plain text, database, or any form you like) when the application is closing or settings are changed. Then load them back in during startup.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜