读写配置文件

来源:互联网 发布:企业协作软件 编辑:程序博客网 时间:2024/04/30 10:23
#region 写入配置文件        /// <summary>        /// 写入配置文件        /// 此方法不永久保存写入的值        /// 程序运行结束自动删除 写入配置文件 项目名.vshost.exe.config 的值        /// </summary>        /// <param name="strNewKey">新的键值</param>        /// <param name="strNewValue">新的Value值</param>        public void AddAppconfig(string strNewKey, string strNewValue)        {            string strAppconfigPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "王鹏翀.exe.config";            try            {                bool bl = false;                foreach (string Key in ConfigurationManager.AppSettings)                {                    if (Key == strNewKey)                    {                        bl = true;                    }                    Configuration config = ConfigurationManager.OpenExeConfiguration(strAppconfigPath);                    if (bl)                    {                        config.AppSettings.Settings.Remove(strNewKey);                    }                    config.AppSettings.Settings.Add(strNewKey, strNewValue);                    config.Save(ConfigurationSaveMode.Modified);                    ConfigurationManager.RefreshSection("appSettings");                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }        }        #endregion  
#region 修改配置文件         /// <summary>        /// 修改配置文件        /// 此方法将写入的值保存 到 项目名.exe.config.config                /// </summary>        /// <param name="strNewKey">新的键值</param>        /// <param name="strNewValue">新的Value值</param>        public void UpdateAppconfig(string strKey, string strNewValue)        {            //获取文件路径            string strAppconfigPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "王鹏翀.exe.config";            //MessageBox.Show(s);            XmlDocument doc = new XmlDocument();            doc.Load(strAppconfigPath);            XmlNodeList node = doc.GetElementsByTagName("add");            for (int i = 0; i < node.Count; i++)            {                XmlAttribute att = node[i].Attributes["key"];                if (att.Value == strKey)                {                    return;                }                else                {                    att = node[i].Attributes["strNewValue"];                    att.Value = strNewValue;                }            }        }        #endregion   
#region 读取配置文件        /// <summary>        /// 方法名 ReadAppconfig        /// 读取配置文件        /// </summary>        /// <param name="strKey">需要读取的键值</param>        /// <returns>读取的Value值</returns>        public string ReadAppconfig(string strKey)        {            string strReadValue = "";            try            {                string strAppconfigPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "王鹏翀.exe.config.config";                XmlDocument doc = new XmlDocument();                doc.Load(strAppconfigPath);                XmlNodeList node = doc.GetElementsByTagName("add");                for (int i = 0; i < node.Count; i++)                {                    XmlAttribute att = node[i].Attributes["key"];                    if (att.Value == strKey)                    {                        att = node[i].Attributes["value"];                        strReadValue = att.Value; break;                    }                }                            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }            return strReadValue;        }        #endregion
//对上文做出修改
#region 读取配置文件        /// <summary>        /// 方法名 ReadAppconfig        /// 读取配置文件        /// </summary>        /// <param name="strKey">需要读取的键值</param>        /// <returns>读取的Value值</returns>        public string ReadAppconfig(string strKey)        {            string strReadValue = "";            try            {                string strAppconfigPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "app.config.config";                XmlDocument doc = new XmlDocument();                doc.Load(strAppconfigPath);                XmlNodeList node = doc.GetElementsByTagName("add");                for (int i = 0; i < node.Count; i++)                {                    XmlAttribute att = node[i].Attributes["key"];                    if (att.Value == strKey)                    {                        att = node[i].Attributes["value"];                        strReadValue = att.Value;                        break;                    }                }                            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }            return strReadValue;        }        #endregion
 #region 写入配置文件        /// <summary>        /// 写入配置文件              /// </summary>        /// <param name="strNewKey">新的键值</param>        /// <param name="strNewValue">新的Value值</param>        public void AddAppconfig(string strNewKey, string strNewValue)        {            string strAppconfigPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "app.config";            try            {                bool bl = false;                foreach (string Key in ConfigurationManager.AppSettings)                {                    if (Key == strNewKey)                    {                        bl = true;                    }                    Configuration config = ConfigurationManager.OpenExeConfiguration(strAppconfigPath);                    if (bl)                    {                        config.AppSettings.Settings.Remove(strNewKey);                    }                    config.AppSettings.Settings.Add(strNewKey, strNewValue);                    config.Save(ConfigurationSaveMode.Modified);                    ConfigurationManager.RefreshSection("appSettings");                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }        }        #endregion
  #region 修改配置文件         /// <summary>        /// 修改配置文件         /// </summary>        /// <param name="strNewKey">新的键值</param>        /// <param name="strNewValue">新的Value值</param>        public void UpdateAppconfig(string strKey, string strNewValue)        {            //获取文件路径            string strAppconfigPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "app.config.config";            //MessageBox.Show(s);            XmlDocument doc = new XmlDocument();            doc.Load(strAppconfigPath);            XmlNodeList node = doc.GetElementsByTagName("add");            for (int i = 0; i < node.Count; i++)            {                XmlAttribute att = node[i].Attributes["key"];                if (att.Value == strKey)                {                    return;                }                else                {                    att = node[i].Attributes["strNewValue"];                    att.Value = strNewValue;                }            }        }        #endregion      
//此处运行时要将app.config配置文件放入bin目录下的Release文件下