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