For desktop applications, it is often necessary to log some user configuration information, and early practice is generally to use the read-write INI file method.
For. NET application, and does not provide the direct operation INI file class, need to call Win32API, specific methods can refer to:
http://www.blogcn.com/user52/seabluescn/blog/23969537.html can see this method is more troublesome.
With the advent of. NET Framerwork 2.0, which provides intrinsic support for application settings, it is now much easier to read and write configuration information.
1. New project, open properties\settings.settings name, type, value, do not say, a look to understand, the only thing to say is the scope,
Application: Program settings, read only;
UESR: User Configuration Properties: readable and writable.
We built two configuration properties, "ConnStr": String type, read-only, left:uint type, readable and writable.
:
2. You can now use these two configuration properties:
<summary>
Reading data
</summary>
private void Btnread_click (object sender, EventArgs e)
{
string connstr = WindowsApplication1.Properties.Settings.Default.ConnStr;
MessageBox.Show (CONNSTR);
}
<summary>
Deposit data
</summary>
private void Btnset_click (object sender, EventArgs e)
{
UINT S = 123;
WindowsApplication1.Properties.Settings.Default.Left = s;
WindowsApplication1.Properties.Settings.Default.Save ();
}
3. The program directory will have a WindowsApplication1.exe.config file, you can directly modify the file to change the configuration.
4. For the User Configuration attribute, the modified value is not stored in the WindowsApplication1.exe.config file, but is maintained in the C:\Documents and Settings directory. The WindowsApplication1.exe.config file retains the default value when the program read configuration fails. For application configuration Properties (application), the values are stored directly within the WindowsApplication1.exe.config file (read-only).
String connstr = Tang. PROPERTIES.SETTINGS.DEFAULT.CONNSTR;
String connstr = properties.settings.default["ConnStr"]. ToString ();
String connstr = configurationmanager.appsettings["ConnStr"];
ConfigurationManager This approach requires adding a reference DLL System.Configuration
int aa = Convert.ToInt32 (Properties.Settings.Default.Left);
Console.WriteLine (AA);
1. Definition
Define the configuration fields in the Settings.settings file. The scope is defined as: User can be changed while running, Applicatiion is not changed when run. Can use the data grid view, very convenient;
2. Read configuration values
Text1.Text = Properties.Settings.Default.FieldName;
FieldName is the field you define
3. Modify and save the configuration
Properties.Settings.Default.FieldName = "Server";
Properties.Settings.Default.Save ();//Use the Save method to save changes
Note: When setting scope to user, his configuration is placed in C:\Documents and Settings\localservice\local Settings\Application Data\ In this directory or in the subdirectory user.config configuration file.
How to use the application configuration in dotnet 2.0 settings.settings