C # Add a configuration file in winform

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.