1. First create a winform Solution
2. add a configuration file in the same way as adding a class file. For example, right-click the project name under solution-> Add-> Create item-> select "application configuration file ",. net default file name is app. config, click OK
Open the app. config file and add the configuration file content, for example:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "author name" value = "sopper"/>
<Add key = "sqlconn" value = "Data Source = (local); Password = sa; user id = sa; initial catalog = DB"/>
</Appsettings>
</Configuration>
3. Right-click "Reference" under solution> "add reference", select "system. Configuration" under ". Net", and click "OK ".
4. Add using system. configuration to the program code. Finally, you can use the following code to use the configuration file.
String appname = configurationsettings. receivettings ["author name"];
String strsql = configurationmanager. etettings ["sqlconn"]; // gets the connection string set in the configuration file.
// String strsql = system. configuration. configurationmanager. connectionstrings [0]. connectionstring; // obtain the default connection string of the system.
Label1.text = appname;
Label2.text = strsql;
Note: the configuration file name here is app. config: When an application is generated, a new config file will be generated under the application directory. The file name is the same as the application file name, And the suffix is. config. (For example, the configuration file name of winform.exeis winform.exe. config) the configuration file used by the application is the config file with the same name as it.