配置文件操作
配置文件一般分为内置配置文和用户自定义配置文件。
内置配置文件包括app.config、web.config、Settings.settings等等。
用户自定义配置文件一般是将配置信息放到XML文件或注册表中,配置信息一般包括程序设置,记录运行信息,保存控件的信息(比如位置,样式)。
一、内置配置文件操作
app.config和web.config操作类似,以app.config为例,Settings.settings能够指定值的类型和范围。
1.app.config文件操作
该配置文件中主要的节点有:connectionStrings、appSettings、configSections等,这几个属于常用,操作都略有不同,DotNet提供直接操作各个节点的方法。在用到ConfigurationManager时要添加system.configuration.dll程序集的引用。
程序移植后配置文件的修改会保存在.exe.config的文件中,但是根据我经验如果你不修改配置文件,一般exe不自动创建一个.exe.config的文件。
在项目进行编译后,在bin\Debuge文件下,将出现两个配置文件,一个名为“*.EXE.config”,另一个名为“*.vshost.exe.config”。第一个文件为项目实际使用的配置文件,在程序运行中所做的更改都将被保存于此;第二个文件为原代码“app.config”的同步文件,在程序运行中不会发生更改。
l connectionStrings:由于保存数据连接字符串。
读:ConfigurationManager.ConnectionStrings["AccessDB"].ConnectionString;
写:
//设置连接字符串
ConnectionStringSettings setConnStr = newConnectionStringSettings("AccessDB", connectionString,"System.Data.OleDb");
//打开当前应用程序的app.config文件,进行操作
Configuration appConfig =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//由于没有更新连接字符串的方法,所以这里直接再添加一个连接字符串
appConfig.ConnectionStrings.ConnectionStrings.Add(setConnStr);
appConfig.Save();
// 强制重新载入配置文件的ConnectionStrings配置节
ConfigurationManager.RefreshSection("connectionStrings");
l appSettings:主要存储程序设置,以键值对的形式出现。
读:String str = ConfigurationManager.AppSettings["DemoKey"];
写:
Configuration cfg=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cfg.AppSettings.Settings["DemoKey"].Value= "DemoValue";
cfg.Save();
l configSections:自定义配置节
name:自定义配置节的名称。
type:自定义配置节的类型,主要包括:
System.Configuration.SingleTagSectionHandler
System.Configuration.DictionarySectionHandler
System.Configuration.NameValueSectionHandler。
不同的type不但设置配置节的方式不一样,最后访问配置文件的操作上也有差异。
三个不同的type操作:
<?xmlversion="1.0" encoding="utf-8" ?><configuration><configSections><sectiontype="System.Configuration.SingleTagSectionHandler"/><sectiontype="System.Configuration.DictionarySectionHandler"/><sectiontype="System.Configuration.NameValueSectionHandler" /></configSections><Test1 setting1="Hello"setting2="World"/><Test2><add key="Hello"value="World" /></Test2><Test3><add key="Hello"value="World" /></Test3></configuration>
说明:在声明部分使用<sectiontype="System.Configuration.SingleTagSectionHandler"/>声明了一个配置节它的名字叫Test1,类型为SingleTagSectionHandler。在设置配置节部分使用 <Test1 setting1="Hello"setting2="World"/>设置了一个配置节,它的第一个设置的值是Hello,第二个值是World,当然还可以有更多。其它的两个配置节和这个类似。
下面我们看在程序中如何访问这些自定义的配置节。我们用过ConfigurationSettings类的静态方法GetConfig来获取自定义配置节的信息。
//访问配置节Test1IDictionary IDTest1 =(IDictionary)ConfigurationSettings.GetConfig("Test1");string str = (string)IDTest1["setting1"]+" "+(string)IDTest1["setting2"];MessageBox.Show(str);//输出Hello World//访问配置节Test1的方法2string[] values1=new string[IDTest1.Count];IDTest1.Values.CopyTo(values1,0);MessageBox.Show(values1[0]+""+values1[1]);//输出HelloWorld//访问配置节Test2IDictionary IDTest2 =(IDictionary)ConfigurationSettings.GetConfig("Test2");string[] keys=new string[IDTest2.Keys.Count];string[] values=new string[IDTest2.Keys.Count];IDTest2.Keys.CopyTo(keys,0);IDTest2.Values.CopyTo(values,0);MessageBox.Show(keys[0]+" "+values[0]);//访问配置节Test3NameValueCollectionnc=(NameValueCollection)ConfigurationSettings.GetConfig("Test3");MessageBox.Show(nc.AllKeys[0].ToString()+""+nc["Hello"]); //输出HelloWorld
配置节处理程序
返回类型
SingleTagSectionHandler
Systems.Collections.IDictionary
DictionarySectionHandler
Systems.Collections.IDictionary
NameValueSectionHandler
Systems.Collections.Specialized.NameValueCollection
l sectionGroup:自定义配置节组
配置节组是使用<sectionGroup>元素,将类似的配置节分到同一个组中。配置节组声明部分将创建配置节的
包含元素,在<configSections>元素中声明配置节组,并将属于该组的节置于<sectionGroup>元素中。下面
哪怕前方的路会充满坎坷,但为梦想而拼搏的人会永不言败