Today, when doing the review, suddenly found that they could not read the configuration file in the database connection string, and checked for half a day did not find out why, and finally to the almighty degree Niang to solve--
1. App.Config profile is added to the project do not modify the name , or the application can not find the file, see Clearly, do not change the name ~ ~
2. Common uses:
We are generally used to do database access, now there are two more common ways, as follows--
(1). Key, Value method
Example: Configuration file section:
<?xml version= "1.0" encoding= "Utf-8"?> <configuration> <appSettings> <add key= "Con" Value= "Server=testname\sql2000;uid=sa;pwd=123;database=test"/>// here the server, UID, PWD, and database are case-insensitive, Database name test is not case sensitive, for example, you can write database=test
</appSettings>
</configuration>
CS section:
String access_str = configurationmanager.appsettings["Con"];
(2). Name, connectionstring mode
Example:
Configuration file section:
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <connectionStrings>
</configuration>
CS section:
String access_str = configurationmanager.connectionstrings["Con"]. ConnectionString;
3.App. Config Profile introduction (reprinted from http://blog.xieyc.com/csharp-read-and-write-configuration-file-app-config/)
We often want to write some configuration information in the program, such as the version number, the connection string for the database, and so on. You may know that you can use Properties.settings to do similar work in WinForm applications, but these actually take advantage of the app. config profile. This article explores how to access the app. Config method in code. The use of App. Config is far more complex than the one mentioned above, so only the most basic AppSettings configuration section is discussed.
(1) configuration file Overview:
The application configuration file is a standard XML file, and XML tags and attributes are case-sensitive. It can be changed as needed, and developers can use the configuration file to change the settings without having to recompile the application. The root node of the profile is the configuration file. We often visit appsettings, which is made up of. NET pre-defined configuration section. The schema of the configuration file that we often use is the form of customer complaint. First of all, there is an impression that the following example will have a clearer understanding. The following "configuration section" can be understood as a node that configures an XML.
Common Configuration file modes:
<configuration>
<configSections>//configuration section Declaration area, including configuration sections and namespace declarations
<section>//configuration section Declaration
<sectionGroup>//Define configuration section groups
Configuration section declarations in <section>//configuration section groups
<appSettings>//Pre-defined configuration section
<custom element for configuration section>//configuration section Settings area
The following is an example of the most common application configuration file, with only the appsettings section:
12345678 |
<?
xml version
=
"1.0" encoding
=
"utf-8"
?>
<
configuration
>
<
appSettings
>
<
add key
=
"connectionstring" value="User Source=.;Password=;Initial
Catalog
=
test
;
Provider
=
SQLOLEDB
.1;" />
<
add key
=
"TemplatePATH" value
=
"Template" />
</
appSettings
>
</
configuration
>
|
In the predefined appSettings section (note case), there are many elements, the names of which are "add", and two attributes are "key" and "value" respectively.
. NET provides access to the appsettings section. In. NET 1.0 and 1.1, you can use the system.configuration.configurationsettings.appsettings["key" to Val the <add> element of key = "Key" The UE attribute is accessed.
Note : It is now clear in the. Net FrameWork 2.0 that this configurationsettings property has been deprecated and it is recommended to change to ConfigurationManager or WebConfigurationManager.
With System.Configuration.ConfigurationManager, you need to add a reference to the System.Configuration.dll assembly in your project. (Right-click the project name in the Solution Manager and select Add Reference from the right-click menu, located under the. NET tab.) )
Once you have added a reference, you can use configurationmanager.appsettings["Key" to read the corresponding value.
However, the Configurationmanager.appsettings property is read-only and does not support modifying property values. This is because it is said that Microsoft does not recommend that we write the app. Config file dynamically, but instead recommends that you configure it manually to make static access when the program is running.
If you really need to make changes in your program, write app. config, please look down.
(2) Read and write operation of appsettings configuration section
The method of reading the AppSettings section of the App. Config file is relatively simple and can be accessed by means of the system.configuration.configurationmanager.appsettings["Key" above. But it has been said earlier that the method does not provide writing.
If you want to write to a configuration file, you can use the ConfigurationManager object to perform an open profile operation, and a configuration object will be returned, which can be manipulated by using the object (and so on).
The implementation code is given below (adding a reference using the System.Configuration namespace)
//get the Configuration objectConfiguration config =System.Configuration.ConfigurationManager.OpenExeConfiguration (Configurationuserlevel.none); //read the value of the <add> element according to key stringname = CONFIG. appsettings.settings["Con"]. Value; //write the value of the <add> elementConfig. appsettings.settings["Con"]. Value ="XIEYC"; //Add <add> elementsConfig. APPSETTINGS.SETTINGS.ADD ("URL","http://www.xieyc.com"); //Delete <add> elementsConfig. AppSettings.Settings.Remove ("name"); //Be sure to remember to save and write the config without parameters. Save () can alsoCONFIG. Save (configurationsavemode.modified); //Refresh, otherwise the program reads the previous value (may have been loaded into memory)System.Configuration.ConfigurationManager.RefreshSection ("appSettings");//the parentheses are fixed values .
It is important to note that:
1. Accessing the <add> element based on a nonexistent key value, or even removing the nonexistent element using the Remove () method, will not cause an exception, which returns NULL.
2, add already existing <add> elements will not cause an exception, but concat the existing value and the new value, with "," separated, for example: "Olldvalue,newvalue".
3, after the project is compiled, under the running directory Bin\debuge file, there will be two configuration files, one named "ProjectName.EXE.config" and the other named "ProjectName.vshost.exe.config". The first file is the configuration file that the project is actually using, and the changes made in the program run will be saved here, and the second file is actually the sync file for "app. Config" in the original code and will not change during the program's run.
4, pay special attention to case (XML file is case-sensitive), such as appsettings configuration section.
5, may have the reader to think, since the App. Config is the standard XML, of course, can also be used to manipulate the general XML file method to read and write. Of course it's possible! You can see the code at the end of the reference [2] and [3], but I think this loses the meaning of the VS Provisioning App. Config file, and it's not as easy as defining a profile yourself.
This article only roughly describes the access method for the appsettings configuration section in the App. Config file, the connectionstrings configuration section is basically similar, and you can customize the configuration section. These advanced usage can be realized by yourself, VS is very powerful in the management of the config file, such as the WinForm application's settings settings (which can be accessed in the IDE or through code), but it also uses the App. config file.
About the app. config configuration file