// 在配置文件app.config中添加字段(在<configuration>节下写)
A) 声明一个节
<configSections>
<section name="ScreenLockInit" type="System.Configuration.NameValueSectionHandler" />
</configSections>
B) 配置节中的值
<ScreenLockInit>
<add key="DISENABLE_CTRL_ESC" value="false"/>
</ScreenLockInit>
//读方法1
//读取XML文件2

/**//// 取配置文件中的键值3
/// <param name="index">配置文件中的索引名称 可视为上例中的ScreenLockInit值 </param>4
/// <param name="key">KEY值 可视为上例中的DISENABLE_CTRL_ESC 值</param>5
/// <returns>返回读取配置文件的值, 可视为上例中的值fasle</returns>6
public static String getCustomValue(String index, String key)7


{8
NameValueCollection nvColl = (NameValueCollection)ConfigurationSettings.GetConfig(index);9
String value = nvColl[key];10
return value;11
}12

13

14
//另一种方法:15
XmlNode tableNode = xmlTable.SelectSingleNode("configuration/custom/table[@name='" + tableName + "']"); //取出表节点16
XmlNode fieldNode = tableNode.SelectSingleNode("field[@name='" + fieldName + "']"); //取出字段节点17
XmlElement fieldElement = (XmlElement)fieldNode; //类型转换18
Hashtable has = new Hashtable();19
if (fieldElement != null) //如果有对应的信息20


{21
has.Add(P_NAME, fieldElement.GetAttribute(P_NAME));//获得属性值22
has.Add(P_TEXT, fieldElement.GetAttribute(P_TEXT)); //获得属性值23
has.Add(P_ISFIELD, fieldElement.GetAttribute(P_ISFIELD)); //获得属性值24
}25

26

//修改方法
1
//修改XML文件(程序运行后会在bin目录下生成一个和exe文件同//名的配置文件,如:项目名为abc,则会生成abc.exe.config, //实际上修改的配置文件是abc.exe.config,而不是app.config)2
XmlDocument xmlDoc = new XmlDocument();3
xmlDoc.Load(Application.ExecutablePath + ".config");//加载配置文件,同名.exe.config配置文件4
XmlElement pwdElement = (XmlElement)xmlDoc.SelectSingleNode("configuration/ScreenLockInit/add[@key='UNLOCK_PASSWORD']"); //取出表节点5
if (pwdElement != null)6


{7
pwdElement.SetAttribute("value","true"); //设置属性值8
xmlDoc.Save(Application.ExecutablePath + ".config");//保存配置9
Properties.Settings.Default.Reload();//刷新10
}11

1
//读取XML文件2

/**//// 取配置文件中的键值3
/// <param name="index">配置文件中的索引名称 可视为上例中的ScreenLockInit值 </param>4
/// <param name="key">KEY值 可视为上例中的DISENABLE_CTRL_ESC 值</param>5
/// <returns>返回读取配置文件的值, 可视为上例中的值fasle</returns>6
public static String getCustomValue(String index, String key)7


{8
NameValueCollection nvColl = (NameValueCollection)ConfigurationSettings.GetConfig(index);9
String value = nvColl[key];10
return value;11
}12
