App. config configuration file

Source: Internet
Author: User
Tags connectionstrings

App. config configuration file

We all know.. NET application development can use configuration files to save frequently-used and possibly changed information, such as the log file storage path and database connection information, in this way, even if the parameter information in the production environment is inconsistent with that in the development environment, you only need to change the configuration file without modifying the source code and re-compiling, making maintenance more convenient. Therefore, the configuration file may be used in many cases.

Configuration files can be divided into two types: application configuration files (App. config), one is the Web configuration file (web. config ). the biggest difference between the two configuration files is that the Web configuration files are updated in real time and the application configuration files are not updated in real time. today we are talking about app. config.

The application configuration file (App. config) is a standard XML file, and the XML tag and attribute are case sensitive. It can be changed as needed. developers can use the configuration file to change the settings without re-compiling the application. Its root node is <configuration>. Common subnodes under the <configuration> node include <configsections>, <etettings>, and <connectionstrings>, this also stipulates that the <etettings> node is mainly used to configure some application configuration information, while the <connectionstrings> node is mainly used to configure the database connection string information.

The procedure for configuring a configuration file is as follows:

Add the app. config file to the project:

Right-click the project name and select "add"> "Add new item". In the displayed "Add new item" dialog box, select "add application configuration file "; if the project has no configuration file before, the default file name is "app. config, and click OK ". In the designer View

The app. config file is:

After the project is compiled, Run "bin \ users. manifest ".

The first file is the configuration file actually used by the project. All changes made during the program running will be saved here. The second file is the original code "app. config.

Connectionstrings configuration section:

Used to configure database connection string information as agreed

Do not add spaces on both sides of the equal sign.

<? XML version = "1.0" encoding = "UTF-8"?>

<Configuration>

<! -- Database connection string -->

<Connectionstrings>

<Add name = "connectionstring" connectionstring = "Server = Local; database = charge_sys; user id = sa; Password = 123456"/>

</Connectionstrings>

</Configuration>

 

To reference this configuration node in a program, perform two steps in advance:

1. Reference namespace: using system. configuration;

2, system. configuratio's naming control requires you to manually introduce your project, right-click your project -- add reference-in the pop-up reference box, in. net reference to find the system. configuration-confirm

3. Before step 3, make sure that the first two steps are unavailable. Public String connectionstring = system. configuration. configurationmanager. connectionstrings ("connectionstring"). tostring ();

String strconn = system. configuration. configurationmanager. connectionstrings ["strconnection"]. connectionstring;

Appsettings configuration section:

The appsettings configuration section is the configuration of the entire program. If you configure the current user, use the usersettings configuration section. The format is the same as the following configuration writing requirements.

<Deleetask>

<! -- Formats of images that can be uploaded -->

<Add key = "imagetype" value = ".jpg?.bmp =.gif=.png#.jpeg"/>

<! -- File type that can be uploaded -->

<Add key = "filetype" value = ".jpg?.bmp =.gif=.png#.?#.##.zip#.rar=.xls#.doc"/>

</Appsettings>

You can access the value in the <deleetask> node according to the key. The following is an example of reading the value of the key "filetype" node:

String filetype = system. configuration. configurationmanager. receivettings ("filetype"). tostring;

Note:

To access the app. config file using the following code, in addition to adding a reference to system. configuration, you must also add a reference to system. configuration. dll in the project.

<Configuration xmlns = "http://schemas.microsoft.com/.NetConfiguration/v2.0">
<Deleetask>
<! -- Enable data caching -->
<Add key = "enablecaching" value = "true"/>
</Appsettings>
<Connectionstrings>
<Add name = "strconnuserdb"
Connectionstring = "Data Source =.; initial catalog = profile1; Integrated
Security = true"
Providername = "system. Data. sqlclient"/>
</Connectionstrings>

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.