对配置文件的一些疑问:
在应用程序的目录下,有两处值得注意的地方,一个是应用程序根目录下的App.config文件,和bin\debug\name.exe.config 或者 bin\Release\name.exe.config下的配置文件。
所谓操作的配置文件是其中的哪个呢?bin\debug中的配置文件是在程序在编译环境中运行时从App.config得到的?
第一种方法:
引入System.Configuration.dll,空间System.Configuration。
应用程序中执行代码:
try
{
showData.Text = System.Configuration.ConfigurationSettings.AppSettings["ConnenctionString"];//读取文件中的节点
}
catch (Exception eee)
{ }
App.config添加一些数据:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="ConnenctionString" value="111"/>
</appSettings>
</configuration>
其中add节点中严格格式写入!!
在开发环境中调试运行,改变name.exe.config中的数据。和App.config一样。
向name.exe.config文件中配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="ConnenctionString" value="111"/>
</appSettings>
</configuration>
会读取到键名为ConnenctionString的对应的值111.
发现:App.config文件中的节被程序读取到时,会把App.config中的数据保存到name.exe.config;如果没有读取到不会保存到name.exe.config中。在name.exe.config中的修改不会变化到App.config中。
ps1:在VS2005中没有默认的配置文件 ,添加方法有手工和设置。
手工:添加App.Config文件,按格式写配置内容
设置项目:如下图