P{uphp{O{p{yOpc{jjpM{VPjOy{j{j{{hP{jppyaAabreErr-LXCWX~~-|F9-P99WP9W

Settings.settings` stores preferences on the disk. Writing a setting to disk when a program exits and then reading it back when the program is started again is cumbersome. We can use instead the Settings.settings file.`480px`300px`Intro.` There are some mechanisms for programmers to easily store settings on the disk, and access them in memory whenever needed. This helps improve speed of development and the feature set of programs. It makes programs faster to develop.`We get started` through the Visual Studio C# development environment. In a typical Windows Forms project, there is a folder called Properties and a file called Settings.settings in that folder. `Next: `We set up settings. We look at the steps you need to take to get this properly configured.`First steps to take.` Select the file. Then, please double click on the Settings.settings file and you should see a settings table. Next, type a name of the variable in the leftmost column. `Then: `In the same row, set the type (int, string). Finally, set the value in the right side.`Understanding application/user.` Select whether it is an application setting or a user setting. This indicates whether the setting will be changed by the user or not. Also, the user setting is reset by each user installation.`Scope column.` There is a Scope column in the table in Visual Studio. There are two options in each cell there: Application and User. Next, we describe the scope values available and their meanings. `Application: `Not changed by the user. It is constant in every instance of the application.`User: `The setting is not constant throughout each instance. It is reset for each user installation.`Code.` The real bonus that C# and .NET provide in this situation is the ease of use of the syntax and the clarity of the required code. Here's what I do when I want to start up the program and retrieve the string and display it in a text box. `There are no complicated` constructors or method calls. When the program is being exited, an event handler called FormClosing is called. Here is how I set the saved input string to the last input string. `Note on FormClosing event.` There is one method call that needs to be made when the program exits. That is easy to do, however, and making a simple FormClosing event handler is sufficient. `Next: `I want to show an example using a different data type. Here's how to retrieve a value from the settings.`Summary.` We saw ways to use the Settings.settings file in Visual Studio with the C# language. The resources are automatically turned into regular properties. This is useful for developing applications quickly. `Review: `Using this convenient mechanism to persist application and user settings. It allows us to focus on the core of an application.

012Code that loads settings 1: C#2 2// 111a 1 from the Setting.settings file show1the above 1screenshot. "SavedInput1" is just a custom variable which can 1be named anything1your program. Set the Text of an input box 1to the property. // 2inputBox.Text1Properties.2Settings2.Default.SavedInput1;2Code that sets property1FormClosing: C#2 1TextWindow_FormClosing(object sender, FormClosingEventArgs e) {2 0// 01We are going1write1the settings1our code. We take the 1s 01of a function,1set the settings 1 equal1it. 0// 02Properties.2Settings2.Default.SavedInput11SanitizeInput(inputBox.Text);2 0// 01Now, we need1save the settings file. If we don't save it, they 01disappear. By saving it, we will be able1use all the settings exactly 01as they are now, the next time the program is run. 0// 02Properties.2Settings2.Default.Save(); }2Code that retrieves setting: C#2 2// 1Retrieve a 1 from the Settings.settings file. The 1 1was manually added1the settings file1the table1Visual Studio. // 2long expireTime1Properties.2Settings2.Default.ExpireMs;2