Your Windows Forms applications will often require data that is critical to running the application, but which you do not want to include directly in the application's code. If your application uses a Web Service or a database server, you may want to store this information in a separate file, so that you can change it in the future without re-compiling. Similarly, your applications may require storing data that is specific to the current user. Most applications, for example, have user preferences that customize the application's appearance and behavior.
Application settings addresses both needs by providing an easy way to store both application-scoped and user-scoped settings on the client computer. Using Visual Studio or a code editor, you define a setting for a given property by specifying its name, data type, and scope (application or user). You can even place related settings into named groups for easier use and readability. Once defined, these settings are persisted and read back into memory automatically at run time. A pluggable architecture enables the persistence mechanism to be changed, but by default, the local file system is used.
Application settings works by persisting data as XML to different configuration (.config) files, corresponding to whether the setting is application-scoped or user-scoped. In most cases, the application-scoped settings are read-only; because they are program information, you will typically not need to overwrite them. By contrast, user-scoped settings can be read and written safely at run time, even if your application runs under partial trust. For more information about partial trust, see Security in Windows Forms Overview.
Settings are stored as XML fragments in configuration files. Application-scoped settings are represented by the <application.Settings> element, and generally are placed in app.exe.config, where app is the name of your main executable file. User-scoped settings are represented by the <userSettings> element and are placed in user.config, where user is the user name of the person currently running the application. You must deploy the app.exe.config file with your application; the settings architecture will create the user.config files on demand the first time the application saves settings for that user. You can also define a <userSettings> block within app.exe.config to provide default values for user-scoped settings.
Custom controls can also save their own settings by implementing the IPersistComponentSettings interface, which exposes the SaveSettings method. The Windows Forms ToolStrip control implements this interface to save the position of toolbars and toolbar items between application sessions. For more information about custom controls and application settings, see Application Settings for Custom Controls.