Chinaunix首页 | 论坛 | 博客
  • 博客访问: 893253
  • 博文数量: 282
  • 博客积分: 10843
  • 博客等级: 上将
  • 技术积分: 2435
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-01 15:23
文章分类

全部博文(282)

文章存档

2013年(1)

2012年(18)

2011年(14)

2010年(30)

2009年(48)

2008年(55)

2007年(55)

2006年(61)

分类:

2010-01-15 17:12:11

    VS2003中对于应用程序配置文件(app.config或者web.config)只提供了读取的功能。而 在VS2005中,对于配置文件的功能有了很大的加强。
    在VS2005中,对于应用程序配置文件的读写一般使用Configuration,ConfigurationManager两个类。
    ConfigurationManager类为客户应用程序提供了一个访问的功能。
    使用ConfigurationManager对象执行打开配置文件的操作后,将会返回一个Configuration的对象。
    通过程序实现读写配置文件的代码如下所示:
1   创建配置文件中的配置节所对应的类。该类必须继承自ConfigurationSection

public sealed class ConfigurationSections : ConfigurationSection
     {
         [ConfigurationProperty("filename", DefaultValue = "default.txt")]
         public string FileName
         {
             get
             {
                 return (string)this["filename"];
             }
             set
             {
                 this["filename"] = value;
             }
         }
     }
     public sealed class BusinessSpaceConfiguration : ConfigurationSection
     {
         [ConfigurationProperty("filename")]
         public string FileName
         {
             get
             {
                 return (string)this["filename"];
             }
             set
             {
                 this["filename"] = value;
             }
         }
     }


2   创建配置文件代码

private static void WriteAppConfiguration()
         {
             try
             {
                 ConfigurationSections configData = new ConfigurationSections();
                 configData.FileName = "abc.txt";
                 System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                 config.Sections.Remove("ConfigurationSections");
                 config.Sections.Add("ConfigurationSections", configData);
                 config.Save();

                 BusinessSpaceConfiguration bsconfigData = new BusinessSpaceConfiguration();
                 bsconfigData.FileName = "def.txt";
                 System.Configuration.Configuration config1 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                 config1.Sections.Remove("BusinessSpaceConfiguration");
                 config1.Sections.Add("BusinessSpaceConfiguration", bsconfigData);
                 config1.Save();
             }
             catch (Exception err)
             {
                 Console.Write(err.Message);
             }
         }


3 生成的配置文件格式如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
     <configSections>
         <section name="BusinessSpaceConfiguration" type="ConsoleApplication1.BusinessSpaceConfiguration, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
         <section name="ConfigurationSections" type="ConsoleApplication1.ConfigurationSections, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
     </configSections>
     <BusinessSpaceConfiguration filename="def.txt" />
     <ConfigurationSections filename="abc.txt" />
</configuration>


4 读取应用程序配置文件

private static void ReadAppConfiguration()
         {
             ConfigurationSections obj1 = ConfigurationManager.GetSection("ConfigurationSections") as ConfigurationSections;
             BusinessSpaceConfiguration obj2 = ConfigurationManager.GetSection("BusinessSpaceConfiguration") as BusinessSpaceConfiguration;
             Console.WriteLine(obj1.FileName);
             Console.WriteLine(obj2.FileName);

         }



阅读(656) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~