操作App.config与Web.config文件 - System.Configuration方式与XMLPath方式, 超强功能类

 

操作App.config与Web.config文件 

[csharp]  view plain copy print ?
  1. public class ConfigureAppConfig  
  2.     {  
  3.         //静态构造,不能实例化  
  4.         static ConfigureAppConfig() { }  
  5.   
  6.         /// <summary>  
  7.         /// 获取AppSettings配置节中的Key值  
  8.         /// </summary>  
  9.         /// <param name="keyName">Key's name</param>  
  10.         /// <returns>Key's value</returns>  
  11.         public static string GetAppSettingsKeyValue(string keyName)  
  12.         {  
  13.             return ConfigurationManager.AppSettings.Get(keyName);  
  14.         }  
  15.   
  16.         /// <summary>  
  17.         /// 获取ConnectionStrings配置节中的值  
  18.         /// </summary>  
  19.         /// <returns></returns>  
  20.         public static string GetConnectionStringsElementValue()  
  21.         {  
  22.             ConnectionStringSettings settings = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"];  
  23.             return settings.ConnectionString;  
  24.         }  
  25.   
  26.         /// <summary>  
  27.         /// 保存节点中ConnectionStrings的子节点配置项的值  
  28.         /// </summary>  
  29.         /// <param name="elementValue"></param>  
  30.         public static void ConnectionStringsSave(string ConnectionStringsName, string elementValue)  
  31.         {  
  32.             System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
  33.             config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = elementValue;  
  34.             config.Save(ConfigurationSaveMode.Modified);  
  35.             ConfigurationManager.RefreshSection("connectionStrings");  
  36.         }  
  37.   
  38.         /// <summary>  
  39.         /// 判断appSettings中是否有此项  
  40.         /// </summary>  
  41.         private static bool AppSettingsKeyExists(string strKey, Configuration config)  
  42.         {  
  43.             foreach (string str in config.AppSettings.Settings.AllKeys)  
  44.             {  
  45.                 if (str == strKey)  
  46.                 {  
  47.                     return true;  
  48.                 }  
  49.             }  
  50.             return false;  
  51.         }  
  52.   
  53.         /// <summary>  
  54.         /// 保存appSettings中某key的value值  
  55.         /// </summary>  
  56.         /// <param name="strKey">key's name</param>  
  57.         /// <param name="newValue">value</param>  
  58.         public static void AppSettingsSave(string strKey, string newValue)  
  59.         {  
  60.             System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
  61.             if (AppSettingsKeyExists(strKey, config))  
  62.             {  
  63.                 config.AppSettings.Settings[strKey].Value = newValue;  
  64.                 config.Save(ConfigurationSaveMode.Modified);  
  65.                 ConfigurationManager.RefreshSection("appSettings");  
  66.             }  
  67.         }  
  68.     }  

------------------------------------------------------

另外,也可用:

[csharp]  view plain copy print ?
  1. public class AppSettings  
  2.    {  
  3.        public static string AppConfig()  
  4.        {  
  5.            return System.IO.Path.Combine(Application.StartupPath, "xxx.exe.config");  
  6.        }  
  7.   
  8.        public static string GetValue(string appKey)  
  9.        {  
  10.            XmlDocument xDoc = new XmlDocument();  
  11.            try  
  12.            {  
  13.                xDoc.Load(AppSettings.AppConfig());  
  14.                XmlNode xNode = xDoc.SelectSingleNode("//appSettings");  
  15.                XmlElement xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");                 
  16.                if (xElem != null)  
  17.                    return xElem.GetAttribute("value");  
  18.                else  
  19.                    return "";  
  20.            }  
  21.            catch   
  22.            {  
  23.                return "";  
  24.            }  
  25.        }  
  26.   
  27.        public static void SetValue(string AppKey, string AppValue)  
  28.        {  
  29.            XmlDocument xDoc = new XmlDocument();  
  30.            xDoc.Load(AppSettings.AppConfig());  
  31.            XmlNode xNode = xDoc.SelectSingleNode("//appSettings");  
  32.            XmlElement xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");                      
  33.          
  34.            if (xElem1 != null)  
  35.            {  
  36.                xElem1.SetAttribute("value", AppValue);  
  37.            }  
  38.            else  
  39.            {  
  40.                XmlElement xElem2 = xDoc.CreateElement("add");  
  41.                xElem2.SetAttribute("key", AppKey);  
  42.                xElem2.SetAttribute("value", AppValue);  
  43.                xNode.AppendChild(xElem2);  
  44.            }  
  45.            xDoc.Save(AppSettings.AppConfig());  
  46.        }  
  47.    }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值