您的位置:首页 > 编程语言 > C#

C#操作配置文件

2011-07-20 20:32 375 查看
C#操作配置文件(自己参考用)2009-08-27 17:01
由于项目中多处需要对配置文件进行操作,如配置信息的读取、更改和写入,需要一个通用的类来对其进行操作和处理。这次的任务就是配置节的一些用法。 这次升级后的开发工具是基于VS2005的,分析了VS2005 新增的一些功能,它自带了一套配置文件中结点等的配置。目前的项目用的配置文件都是系统自带的,因此,这次的任务主要是对VS2005配置类的一些介绍及 扩充(用户自定义配置节点的操作)。任务分为两个大方向,一个是针对Web项目(对应的配置文件为web.config),另一个是针对WinForm项 目。下面的章节主要概要地介绍一下VS2005中配置项的用法
<!--[if !supportLists]-->1. <!--[endif]-->Web 项目
1.1 系统自带

在VS2005中,我们可以通过系统自带的管理类来读取和修改相应Key所对应的Value值,具体的方法如下(注意:由于key是之读属性,因此只能修改与key相对应的Value值):

传入相应的Key值(如Test),通过调用ConfigurationManager.AppSettings["Test"] .Value即可获得对应的Value值

1) 打开配置文件(Web.config)
   Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
2) 获取相应的appSettings配置节
 AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
3) 增加相应的appSettings配置节
  增加 Key – Value 的键值对(如 ”Test” – “Hello”) appSection.Settings.Add("Test", "Hello");
4) 保存配置节
config.Save(ConfigurationSaveMode.Modified)

1) 打开配置文件(Web.config)
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
2) 传入Key值通过调用下面方法来获取对应的值
如传入Key为Test参数来获取对应值:webConfig.AppSettings.Settings["Test"].Value
3) 保存配置节
config.Save(ConfigurationSaveMode.Modified)
保存的构造函数有三种:
1、不带参数
2、带一个ConfigurationSaveMode参数
3、带一个ConfigurationSaveMode参数和bool型参数
ConfigurationSaveMode的参数有三种方式:
Full
将所有属性都写到配置文件。在创建信息配置文件时或将配置值从一台计算机移动到另一台计算机时最有用

Minimal
仅将不同于继承值的属性写出到配置文件。

Modified
仅将修改的属性写出到配置文件,即使值和继承值相同。

1) 打开配置文件(Web.config)
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
2) 根据传入Key值来删除相应的配置节
如传入Key为Test参数来删除配置节:. webConfig.AppSettings.Settings.Remove(key);
3) 保存配置节
config.Save()

<!--[if !supportLists]--> <!--[endif]--> 在VS2005中系统的配置文件允许写入两个相同key的配置节,如:

<add key ="App" value ="aaaaa"/>
<add key ="App" value ="aaaaa1"/>
读在取的时候获取的是后者的配置节信息

传入相应的name值(如Test),通过调用WebConfigurationManager.ConnectionStrings["Test"].Name即可获得对应的Name值

1) 打开配置文件(Web.config)
   Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~");
2) 创建一个相应的connectionStrings配置节
ConnectionStringSettings test = new ConnectionStringSettings("test1","test1","test1.dll");
3) 增加相应的connectionStrings配置节
webConfig.ConnectionStrings.ConnectionStrings.Add(test);
4) 保存配置节
webConfig.Save()

1) 打开配置文件(Web.config)
   Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~");
2) 传入name值通过调用下面方法来获取对应的值
  webConfig.ConnectionStrings.ConnectionStrings[name].ConnectionString = connectionString;
3) 保存配置节
webConfig.Save(ConfigurationSaveMode.Modified)

1) 打开配置文件(Web.config)
Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~");
2) 根据传入name值来删除相应的配置节
webConfig.ConnectionStrings.ConnectionStrings.Remove(name);
3) 保存配置节

config.Save()

1.2 用户自定义(configSections)
<!--[if !supportLists]-->A. <!--[endif]-->新建一个自定义配置节
1) 打开配置文件(Web.config)
    Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~");
2) 创建一个相应的自定义配置节
SqlExample example = new SqlExample();
Example. Name = “test”;
webConfig.Sections.Add("UserSection", example as ConfigurationSection);
(注:这里的UserSection就是我们定义的配置节的名称)
3) 保存配置节
config.Save()
<!--[if !supportLists]-->B. <!--[endif]-->修改一个自定义配置节
1) 打开配置文件(Web.config)
    Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~");
2) 创建一个相应的自定义配置节
webConfig.Sections.Remove("UserSection ");
SqlExample example = new SqlExample();
Example. Name = “test”;
webConfig.Sections.Add("UserSection", example as ConfigurationSection);
注:这里的UserSection就是我们定义的配置节的名称)
3) 保存配置节
webConfig.Save()
<!--[if !supportLists]-->C. <!--[endif]-->删除一个自定义配置节
1) 打开配置文件(Web.config)
Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("~");
2) 创建一个相应的自定义配置节
webConfig.Sections.Remove("UserSection");

(注:这里的UserSection就是我们定义的配置节的名称)
3) 保存配置节
webConfig.Save()
<!--[if !supportLists]-->D. <!--[endif]-->
<!--[if !supportLists]--> <!--[endif]-->由于自定义配置节的属性是只读的,因而修改实际上是将原来的配置节删除再添加上一个新的

1.3 操作类
除了用户自定义的操作类外,已将appSettings和connectionStrings做好,类中的方法见帮助手册
<!--[if !supportLists]-->2.WinForm 项目
2.1 系统自带

在VS2005中,我们可以通过系统自带的管理类来读取和修改相应Key所对应的Value值,具体的方法如下(注意:由于key是之读属性,因此只能修改与key相对应的Value值):

传入相应的Key值(如Test),通过调用ConfigurationManager.AppSettings["Test"] .Value即可获得对应的Value值

1) 创建一个Configuration实例
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
2) 传入Key值通过调用下面方法来获取对应的值
如传入Key为Test参数来获取对应值:config.AppSettings.Settings["Test"].Value
3) 保存配置节
config.Save(ConfigurationSaveMode.Modified)
保存的构造函数有三种:
不带参数
带一个ConfigurationSaveMode参数
带一个ConfigurationSaveMode参数和bool型参数
ConfigurationSaveMode的参数有三种方式:
Full
将所有属性都写到配置文件。在创建信息配置文件时或将配置值从一台计算机移动到另一台计算机时最有用

Minimal
仅将不同于继承值的属性写出到配置文件。

Modified
仅将修改的属性写出到配置文件,即使值和继承值相同。

1) 打开配置文件(Web.config)
Configuration config = ConfigurationManager.OpenWebConfiguration("~");
2) 根据传入Key值来删除相应的配置节
如传入Key为Test参数来删除配置节:. config.AppSettings.Settings.Remove(key);
3) 保存配置节
config.Save()

<!--[if !supportLists]--> <!--[endif]-->在VS2005中系统的配置文件允许写入两个相同key的配置节,如:
<add key ="App" value ="aaaaa"/>
<add key ="App" value ="aaaaa1"/>
读在取的时候获取的是后者的配置节信息
<!--[if !supportLists]--> 在VS2005中系统的配置文件写入两个相同key和Value的配置节,那么系统会将第二次的Value值追加第一个Value值的后面,并以逗号分隔开,如两次写入
<add key ="App" value ="aaaaa"/> 得到的结果为 <add key ="App" value ="aaaaa,aaaaa"/>

传入相应的name值(如Test),通过调用ConfigurationManager.ConnectionStrings["Test"].Name即可获得对应的Name值

1) 打开配置文件(Web.config)
Configuration config = ConfigurationManager.OpenWebConfiguration("~");
2) 创建一个相应的connectionStrings配置节
ConnectionStringSettings test = new ConnectionStringSettings("test1","test1","test1.dll");
3) 增加相应的connectionStrings配置节
config.ConnectionStrings.ConnectionStrings.Add(test);
4) 保存配置节
config.Save()

1) 打开配置文件(Web.config)
Configuration webConfig = ConfigurationManager.OpenWebConfiguration("~");
2) 传入name值通过调用下面方法来获取对应的值
config.ConnectionStrings.ConnectionStrings[name].ConnectionString = connectionString;
3) 保存配置节
config.Save(ConfigurationSaveMode.Modified)

1) 打开配置文件(Web.config)
Configuration config = ConfigurationManager.OpenWebConfiguration("~");
2) 根据传入name值来删除相应的配置节
config.ConnectionStrings.ConnectionStrings.Remove(name);
3) 保存配置节
config.Save()

2.2 用户自定义(configSections)
在VS2005中,允许用户自己来定义配置文件,具体的操作流程如下:
Configuration)

在类的开头添加 System.Configuration和System.Web.Configuration的引用

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

在红色标记的属性项一栏中,有多个参数可以设置,
Name : 属性名 (string)
DefaultValue : 该属性的缺省值 (string)
IsDefaultCollection : 指示该属性的缺省值是否为一个集合(bool)
IsKey : 指示该属性是否是一个关键属性(bool)
IsRequired : 指示该属性是否是必须的(bool)
Options : 指示该属性是否为一个元素
通常我们只要用到 Name、DefaultValue这两个参数

1) 打开配置文件(App.config


Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
2) 创建一个相应的自定义配置节
SqlExample example = new SqlExample();
Example. Name = “test”;
config.Sections.Add("UserSection", example as ConfigurationSection);
(注:这里的UserSection就是我们定义的配置节的名称)
3) 保存配置节
config.Save()
3) 生成的配置文件的内容如下

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="DataSetting" type="Examples.DataInfo, Examples, Version=.0,   Culture=neutral, PublicKeyToken=null" />
</configSections>
<DataSetting name="This is a Test!" />
</configuration>

1) 打开配置文件(App.config)
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
2) 创建一个相应的自定义配置节
config.Sections.Remove("UserSection ");
SqlExample example = new SqlExample();
Example. Name = “test”;
config.Sections.Add("UserSection", example as ConfigurationSection);
(注:这里的UserSection就是我们定义的配置节的名称)
3) 保存配置节
config.Save()

1) 打开配置文件(App.config)
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
2) 创建一个相应的自定义配置节
config.Sections.Remove("UserSection");
(注:这里的UserSection就是我们定义的配置节的名称)
3) 保存配置节
config.Save()

<!--[if !supportLists]--> 由于自定义配置节的属性是只读的,因而修改实际上是将原来的配置节删除再添加上一个新的
2.3 操作类

除了用户自定义的操作类外,已将appSettings和connectionStrings做好,类中的方法见帮助手册
<!--[if !supportLists]-->3. 总结
如果手动写配置文件的时候有一点值得注意,就是在配置文件App.config或Web.config中写的时候如果有configSections配置节必须将其要放appSettings和connectionStrings配置节的上面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: