配置文件修改

最近在学习C#的我winform开发

在其中发现一个问题  我添加了一个配置文件 相对的 在BIN文件夹中添加了一个   XXX.exe.config文件 但是采用下面的类方法修改的时候发现项目里面的配置文件没被修改,但是BIN文件中的配置文件被修改了 哎

原来 BIN文件夹中的配置文件是项目中配置文件的一个实例 当重新生成之后 BIN文件夹中的配置文件将会还原和项目中的配置文件一样 吼吼

 

public class AppSettingsHelper
    {
        public static string docName = String.Empty;
        private static XmlNode node = null;


        /// <summary>
        /// 设置节点的值,若该节点不存在,则创建一个新的节点。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cfgType"></param>
        /// <returns></returns>
        public static bool SetValue(string key, string value)
        {
            XmlDocument cfgDoc = new XmlDocument();
            loadConfigDoc(cfgDoc);
            // 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;
            }
        }
        /// <summary>
        /// 获取节点的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="cfgType"></param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            XmlDocument cfgDoc = new XmlDocument();
            loadConfigDoc(cfgDoc);
            // 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;
            }
        }

 

        /// <summary>
        /// 移除节点
        /// </summary>
        /// <param name="elementKey"></param>
        /// <param name="cfgType"></param>
        /// <returns></returns>
        public static bool RemoveElement(string elementKey)
        {
            try
            {
                XmlDocument cfgDoc = new XmlDocument();
                loadConfigDoc(cfgDoc);
                // 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;
            }
        }


        /// <summary>
        /// 修改节点的值
        /// </summary>
        /// <param name="elementKey"></param>
        /// <param name="cfgType"></param>
        /// <returns></returns>
        public static bool ModifyElement(string elementKey)
        {
            try
            {
                XmlDocument cfgDoc = new XmlDocument();
                loadConfigDoc(cfgDoc);
                // 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)
        {
            // load the config file  

            docName = ((Assembly.GetEntryAssembly()).GetName()).Name;
            docName += ".exe.config";

            cfgDoc.Load(docName);
            return cfgDoc;
        }

    }

转载于:https://www.cnblogs.com/Rock-Lee/archive/2012/08/25/2656790.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值