App.config 两种版本读取方式(appSettings 和 applicationSettings 的区别)

转自:http://www.ithao123.cn/content-8222094.html

摘要:App.config 两种版本读与体式格局( appSettings 战 applicationSettings 的差别 ) ApplicationSettings是.net 2.0以后涌现的,appSettings是.net 1.1的体式格局. 实在.net 2.0以后两个皆能够用 体式格局一:.net2.0 之前appSe] 


App.config 两种版本读取方式(appSettings 和 applicationSettings 的区别)
 
ApplicationSettings是.net 2.0之后出现的,appSettings是.net 1.1的方式. 其实.net 2.0之后两个都可以用
 
方式一:.net2.0 之前 appSettings
 xml 内容:
 
   
 
 
   
 
 
读取方式:
string sConnectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
string sPath = ConfigurationManager.AppSettings["logpath"]
 
VS2005中用第一种读取方式,需要手动添加app.config内容(appSettings字段),
而且使用ConfigurationManager时要加上 using System.Configuration; 同时添加System.Configuration.dll引用
还有一点就是应用程序目录必须存在MyProject.exe.config文件,否则exe会打不开!
 
方式二: .net2.0之后 applicationSettings
 xml 内容(参数直接在 Project->  Properties ->Settings界面设置,更方便):
   
   
      providerName="System.Data.SqlClient" />
   
   
       
           
                http://localhost/MyWebSrv/Service.asmx
           
       
   
   
       
           
                D:\log.txt
           
       
   
 
读取方式:
 string sConnectionString = MyProject .Properties.Settings.Default. connString;
 string sUrl =  MyProject .Properties.Settings.Default. WebSrv ;    
 string sPat  = MyProject .Properties.Settings.Default.LogPath;  
 
使用第二种方式的优点是直接可以在 Settings界面中编辑内容,还可以构建和测试connectionString字段, 参数配置更直观.
应用程序目录不必一定要有MyProject.exe.config文件,如果没有该文件,读出的值是编译时设置的值,否则读取该文件中的值.

你可能感兴趣的:(WinForm,C#)