C # reads the contents of the. config file

Source: Internet
Author: User

Today in the project, because the program at the same time to start a variety of data, the test is divided into more than one person, you need to separate the data, so with a temporary configuration file, let the test in the configuration file to modify the corresponding data so that each test does not affect each other!

Steps:

First step: Add an app. Config file with the following file format:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<appSettings>
<add key= "Canshu" value= "hkg999$sha777!1002*1003#sha888!0001*0002"/>
</appSettings>
</configuration>

This is an example I used when testing!

Second step: Add the file to the project.

At first I was using this method to read:

String Ceshi =configurationmanager.appsettings["Canshu"];

Note: You need to add the System.Configuration in the Resource Manager reference, and then add the using System.Configuration in the code;

Now the problem is, if you have this app. config file is not in your startup entry under the directory, so you can not get the data you want, return a null.

Workaround: (Get profile data below the absolute path)

The following is a better use of this method:

Execonfigurationfilemap map = new Execonfigurationfilemap ();
Map.   Execonfigfilename = @ "app. Config"; ////(@ "" in the quotation marks is the absolute path of your configuration file in the program).
Configuration config = configurationmanager.openmappedexeconfiguration (map, Configurationuserlevel.none);
string Ceshi = config. appsettings.settings["Canshu"]. Value;

This way you get the data you want! I hope to meet the same problem of beginners like me can get help!

C # reads the contents of the. config file

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.