using System;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Xml;
/* 编写日期: 2008.4.4
* 更新日期:
* 编写人 :James.Chen
* 修改历史及原因:
*
*/
namespace AppFrameWork
{
///
/// 配置文件类型,是WinForm还是WebForm
///
public enum ConfigFileType
{
WebConfig,
AppConfig
}
///
/// 对AppSettings节点进行增加,删除,修改操作.
///
public class AppSettingsHelper
{
public static string docName = String.Empty;
private static XmlNode node = null;
///
/// 设置节点的值,若该节点不存在,则创建一个新的节点。
///
///
///
///
///
public static bool SetValue(string key, string value, ConfigFileType cfgType)
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc, cfgType);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new InvalidOperationException("appSettings section not found");
}
try
{
// XPath select setting "add" element that contains this key
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (addElem != null)
{
addElem.SetAttribute("value", value);
}
// not found, so we need to add the element, key and value
else
{
XmlElement entry = cfgDoc.CreateElement("add");
entry.SetAttribute("key", key);
entry.SetAttribute("value", value);
node.AppendChild(entry);
}
//save it
saveConfigDoc(cfgDoc, docName);
return true;
}
catch
{
return false;
}
}
///
/// 获取节点的值
///
///
///
///
public static string GetValue(string key, ConfigFileType cfgType)
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc, cfgType);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new InvalidOperationException("appSettings section not found");
}
// XPath select setting "add" element that contains this key
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (addElem != null)
{
return addElem.GetAttribute("value");
}
// not found, so we need to add the element, key and value
else
{
throw new ArgumentException(string.Format("key '{0}' not found", key));
}
}
private static void saveConfigDoc(XmlDocument cfgDoc, string cfgDocPath)
{
try
{
XmlTextWriter writer = new XmlTextWriter(cfgDocPath, null);
writer.Formatting = Formatting.Indented;
cfgDoc.WriteTo(writer);
writer.Flush();
writer.Close();
return;
}
catch
{
throw;
}
}
///
/// 移除节点
///
///
///
///
public static bool RemoveElement(string elementKey, ConfigFileType cfgType)
{
try
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc, cfgType);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new InvalidOperationException("appSettings section not found");
}
// XPath select setting "add" element that contains this key to remove
node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
saveConfigDoc(cfgDoc, docName);
return true;
}
catch
{
return false;
}
}
///
/// 修改节点的值
///
///
///
///
public static bool ModifyElement(string elementKey, ConfigFileType cfgType)
{
try
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc, cfgType);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new InvalidOperationException("appSettings section not found");
}
// XPath select setting "add" element that contains this key to remove
node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
saveConfigDoc(cfgDoc, docName);
return true;
}
catch
{
return false;
}
}
private static XmlDocument loadConfigDoc(XmlDocument cfgDoc, ConfigFileType cfgType)
{
// load the config file
if (cfgType == ConfigFileType.AppConfig)
{
docName = ((Assembly.GetEntryAssembly()).GetName()).Name;
docName += ".exe.config";
}
else
{
docName = HttpContext.Current.Server.MapPath("web.config");
}
cfgDoc.Load(docName);
return cfgDoc;
}
}
}
App.config:
读操作:
bool maximized = false;
bool.TryParse(AppSettingsHelper.GetValue("maximized", ConfigFileType.AppConfig), out maximized);
if (maximized)
this.WindowState = FormWindowState.Maximized;
写操作:
if(this.WindowState==FormWindowState.Maximized)
AppSettingsHelper.SetValue("maximized","True", ConfigFileType.AppConfig);
else
AppSettingsHelper.SetValue("maximized", "False", ConfigFileType.AppConfig);
其他操作略
本文作者:陈锋的专栏 来源:http://blog.csdn.net/hb_cattle/archive/2008/04/04/
CIO之家 www.ciozj.com 微信公众号:imciow