首页  ·  知识 ·  编程语言
对appSettings节点进行新增、修改、删除、读取操作的类AppSettingsHelper
陈锋的专栏  http://blog.csdn.net/hb_cattle/archive/2008/04/04/  .NET  编辑:dezai  图片来源:网络
using System;using System.Configuration;using System.Reflection;using System.Web;using System.Xml; /* 编写日期: 2008

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
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读