Home
Search
Settings.settings in Visual StudioUse the Settings.settings file in Visual Studio to represent preferences.
C#
This page was last reviewed on Oct 8, 2022.
Settings.settings stores preferences on the disk. This file can be used for writing a setting to disk when a program exits, and then reading it back when the program is started again.
A note. There are some mechanisms for programmers to easily store settings on the disk, and access them in memory whenever needed. Settings.settings makes programs faster to develop.
Settings code. This example code is run when we start up the program. We retrieve the string and display it in a text box in Windows Forms.
Closing When the program is being exited, an event handler called FormClosing is called.
And We set saved input string to the last input string when the FormClosing event handler is invoked.
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;
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(); }
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.
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;
Getting started. 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.
Then Select the file. Double click on the Settings.settings file and you should see a settings table.
Finally Enter a name of the variable in the leftmost column. In the same row, set the type (int, string). Set the value in the right side.
Application, user. This setting indicates whether the setting will be changed by the user or not. Also, the user setting is reset by each user installation.
Scope. There is a Scope column in the table. 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.
A summary. We used the Settings.settings file in Visual Studio with the C# language. The resources are automatically turned into regular properties.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 8, 2022 (grammar).
Home
© 2007-2022 sam allen.
see site info on the changelog.