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;
}
}
}
{
[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);
}
}
{
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>
<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>
3 读取应用程序配置文件
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);
}
{
ConfigurationSections obj1 = ConfigurationManager.GetSection("ConfigurationSections") as ConfigurationSections;
BusinessSpaceConfiguration obj2 = ConfigurationManager.GetSection("BusinessSpaceConfiguration") as BusinessSpaceConfiguration;
Console.WriteLine(obj1.FileName);
Console.WriteLine(obj2.FileName);
}
本文转自
http://hi.baidu.com/geminiboy2008/blog/item/e59dee2a1f441899023bf647.html