技术开发 频道

.NET中读写config文件的各种方法解析

  config文件 - 读与写

  前面我逐个介绍了4种自定义的配置节点的实现类,下面再来看一下如何读写它们。

  读取配置参数:

MySection1 mySectioin1 = (MySection1)ConfigurationManager.GetSection("MySection111");
txtUsername1.Text
= mySectioin1.UserName;
txtUrl1.Text
= mySectioin1.Url;
MySection2 mySectioin2
= (MySection2)ConfigurationManager.GetSection("MySection222");
txtUsername2.Text
= mySectioin2.Users.UserName;
txtUrl2.Text
= mySectioin2.Users.Password;
MySection3 mySection3
= (MySection3)ConfigurationManager.GetSection("MySection333");
txtCommand1.Text
= mySection3.Command1.CommandText.Trim();
txtCommand2.Text
= mySection3.Command2.CommandText.Trim();
MySection4 mySection4
= (MySection4)ConfigurationManager.GetSection("MySection444");
txtKeyValues.Text
= string.Join("\r\n",
                        (from kv
in mySection4.KeyValues.Cast<MyKeyValueSetting>()
                         let s
= string.Format("{0}={1}", kv.Key, kv.Value)
                        
select s).ToArray());

   小结:在读取自定节点时,我们需要调用ConfigurationManager.GetSection()得到配置节点,并转换成我们定义的配置节点类,然后就可以按照强类型的方式来访问了。

  写配置文件:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

MySection1 mySectioin1
= config.GetSection("MySection111") as MySection1;
mySectioin1.UserName
= txtUsername1.Text.Trim();
mySectioin1.Url
= txtUrl1.Text.Trim();

MySection2 mySection2
= config.GetSection("MySection222") as MySection2;
mySection2.Users.UserName
= txtUsername2.Text.Trim();
mySection2.Users.Password
= txtUrl2.Text.Trim();

MySection3 mySection3
= config.GetSection("MySection333") as MySection3;
mySection3.Command1.CommandText
= txtCommand1.Text.Trim();
mySection3.Command2.CommandText
= txtCommand2.Text.Trim();

MySection4 mySection4
= config.GetSection("MySection444") as MySection4;
mySection4.KeyValues.Clear();

(from s
in txtKeyValues.Lines
     let p
= s.IndexOf('=')
     where p > 0
    
select new MyKeyValueSetting { Key = s.Substring(0, p), Value = s.Substring(p + 1) }
).ToList()
.ForEach(kv
=> mySection4.KeyValues.Add(kv));

config.Save();

   小结:在读取自定节点时,我们需要调用ConfigurationManager.GetSection()得到配置节点,并转换成我们定义的配置节点类,然后就可以按照强类型的方式来访问了。

  写配置文件:

  小结:在修改配置节点前,我们需要调用ConfigurationManager.OpenExeConfiguration(),然后调用config.GetSection()在得到节点后,转成我们定义的节点类型,然后就可以按照强类型的方式来修改我们定义的各参数项,最后调用config.Save();即可。

  注意:

  ①.net为了优化配置节点的读取操作,会将数据缓存起来,如果希望使用修改后的结果生效,您还需要调用ConfigurationManager.RefreshSection(".....")

  ② 如果是修改web.config,则需要使用 WebConfigurationManager

  读写 .net framework中已经定义的节点

  前面一直在演示自定义的节点,那么如何读取.net framework中已经定义的节点呢?

  假如我想读取下面配置节点中的发件人。

<system.net>
    
<mailSettings>
        
<smtp from="Fish.Q.Li@newegg.com">
            
<network />
        
</smtp>
    
</mailSettings>
</system.net>

   读取配置参数:

SmtpSection section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
labMailFrom.Text
= "Mail From: " + section.From;

   写配置文件:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
SmtpSection section
= config.GetSection("system.net/mailSettings/smtp") as SmtpSection;
section.From
= "Fish.Q.Li@newegg.com2";
config.Save();

 

0
相关文章