C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: We set up settings. We look at the steps you need to take to get this properly configured.
Then: In the same row, set the type (int, string). Finally, set the value in the right side.
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 that loads settings string: C#
//
// Read in a value from the Setting.settings file show in the above
// screenshot. "SavedInputString" is just a custom variable which can
// be named anything in your program. Set the Text of an input box
// to the property.
//
inputBox.Text = Properties.Settings.Default.SavedInputString;
Code that sets property in FormClosing: C#
void TextWindow_FormClosing(object sender, FormClosingEventArgs e)
{
//
// We are going to write to the settings in our code. We take the results
// of a function, and set the settings string equal to it.
//
Properties.Settings.Default.SavedInputString = SanitizeInput(inputBox.Text);
//
// Now, we need to save the settings file. If we don't save it, they
// disappear. By saving it, we will be able to use all the settings exactly
// as they are now, the next time the program is run.
//
Properties.Settings.Default.Save();
}
Next: I want to show an example using a different data type. Here's how to retrieve a value from the settings.
Code that retrieves setting: C#
//
// Retrieve a value from the Settings.settings file. The string
// was manually added to the settings file in the table in Visual Studio.
//
long expireTime = Properties.Settings.Default.ExpireMs;
Review: Using this convenient mechanism to persist application and user settings. It allows us to focus on the core of an application.