Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)delv
published in(发表于) 2014/1/10 6:23:12
.NET,2.0,中对配置文件的读写_[Asp.Net教程]

.NET 2.0 中对配置文件的读写_[Asp.Net教程]

在基于 .net 2.0 的企业库中,原来的配置应用程序块被废除了,使用了 .net 2.0 自带的读写配置功能,下面我们就来看看 .net 2.0 中读写配置的功能。 即: ConfigurationManager 类
  注意:ConfigurationManager 是处理客户端应用程序配置文件的首选方法;不推荐使用任何其他方法。

  对于 Web 应用程序,建议使用 WebConfigurationManager 类。

  这个类的 AppSettings 属性 在以前1.0 的时候,就有了, 2.0 中增加了 ConnectionStrings 属性。

  这些都不是今天我们要探讨的内容,我们今天要探讨的内容,是把一个配置类保存到配置文件中,以及把这个配置类从配置文件中实例化出来。

  这个配置类,必须是 派生自System.Configuration.ConfigurationSection 类

  如下面的类就是一个配置类


using System.Text;
using System.Configuration;
namespace ConfigTest
{
 class ConfigDataClass : ConfigurationSection
 {
  public ConfigDataClass()
  { }

  [ConfigurationProperty("id")]
  public int ID{
   get{return (int)this["id"];}
   set{ this["id"] = value;}
  }

  [ConfigurationProperty("name")]
  public string Name{
   get{ return this["name"].ToString();}
   set{ this["name"] = value;}
  }

  public override string ToString(){
   StringBuilder info = new StringBuilder();
   info.AppendFormat("id = {0};name = {1}", ID, Name);
   return info.ToString();
  }
 }
}

来源:网络







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759