Sheck mo'see

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

C#操作配置文件

一、应用程序配置文件举例:

代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="ToDataBase"
connectionString
="……."
providerName
="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
<add key="TimeSpan" value="60"/>
<add key="YaxisMax" value="40000"/>
<add key="YaxisMin" value="-40000"/>
<add key="YstepDiv" value="10"/>
</appSettings>
</configuration>

<connectionStrings>节点

配置文件中的<connectionStrings>存储数据库连接字符串信息;

读取方式:String connectString = ConfigurationManager.ConnectionStrings["ToDataBase"].ConnectionString;

<appSettings>节点

存储应用程序运行中的数据信息,也可包括数据库信息;

读取方式:String str = ConfigurationManager.AppSettings["TimeSpan"];

修改方式(列举三种):

a) ConfigurationManager.AppSettings.Set( key, value);
b) Configuration config
= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings[key].Value
= value;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(
"appSettings");
c) Configuration config
= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, newvalue);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(
"appSettings");

 

二、外部配置文件举例01:

<?xml version="1.0" encoding="utf-8"?>
<Config>
<appSettings>
<add key="SetAsDefault" value="False" />
<add key="TimeSpan" value="110" />
</appSettings>
</Config>

(节点名称自定义)

<Config>是配置的根节点;

<appSettings>是Config的子节点;

<add>是appSettings的子节点,key和value是节点add的属性。 

读取方式:

代码
XmlDocument xd = new XmlDocument();
xd.Load(
"Config.xml");
XmlNode root
= (XmlNode)xd.DocumentElement;
// root = xd.SelectSingleNode("Config/appSettings");
XmlNode node = root.SelectSingleNode("appSettings");
XmlNodeList xns
= node.ChildNodes;
foreach (XmlNode xn in xns)
{
str1
= xn.Attributes["key"].Value;
str2
= xn.Attributes["value"].Value;
//
}

 

修改方式:

foreach (XmlNode xn in xns)
{
xn.Attributes[
"key"].Value = str1;
xn.Attributes[
"value"].Value = str2;
……
}
xd.Save(
"Config.xml");

 

三、外部配置文件02:

代码
<?xml version="1.0" encoding="utf-8" ?>
<Parameters>
<SerialPort>
<PortName>COM1</PortName>
<BaudRate>115200</BaudRate>
<DataBits>8</DataBits>
<Parity>NONE</Parity>
<StopBits>1</StopBits>
</SerialPort>
<Show>
<TimeSpan>60</TimeSpan>
<YaxisMax>40000</YaxisMax>
<YaxisMin>-40000</YaxisMin>
<YstepDiv>10</YstepDiv>
</Show>
</Parameters>

 

TEST1:

代码
XmlDocument document = new XmlDocument();
document.Load(Application.StartupPath
+ @"\Config.xml");
XmlNode root
= document.SelectSingleNode("Parameters/SerialPort");
XmlNodeList nodes
= root.SelectNodes("SerialPort");
MessageBox.Show(nodes.InnerText);
// InnerText是所有节点或子节点的值串联(对于XmlNode和XmlNodeList有所不同)
// 结果:COM11152008NONE1

 

TEST2:

代码
XmlDocument document = new XmlDocument();
document.Load(Application.StartupPath
+ @"\Config.xml");
XmlNode root
= (XmlNode)document.DocumentElement;
XmlNode node
= root.SelectSingleNode("SerialPort/PortName");
MessageBox.Show(node.InnerText);
// 结果:COM1

 

posted on 2010-09-18 19:36  Sheck mo'see  阅读(119)  评论(0)    收藏  举报