读写系统配置文件的Key值

前端之家收集整理的这篇文章主要介绍了读写系统配置文件的Key值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

直接上代码


  1. <pre name="code" class="csharp">/// <summary>
  2. /// 读写系统配置xml文件
  3. /// </summary>
  4. public class ConfigurationFile
  5. {
  6. /// <summary>
  7. /// 写入key值
  8. /// </summary>
  9. public static bool SetKeyValue(string key,string value)
  10. {
  11. //增加内容写在appSettings段下 <add key="RegCode" value="0"/>
  12. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  13.  
  14. try
  15. {
  16. if (config.AppSettings.Settings[key] == null)
  17. {
  18. config.AppSettings.Settings.Add(key,value);
  19. }
  20. else
  21. {
  22. config.AppSettings.Settings[key].Value = value;
  23. }
  24. config.Save(ConfigurationSaveMode.Modified);
  25. ConfigurationManager.RefreshSection("appSettings");
  26. }
  27. catch (Exception)
  28. {
  29. return false;
  30. }
  31.  
  32. return true;
  33. }
  34.  
  35. /// <summary>
  36. /// 读取指定key的值
  37. /// </summary>
  38. public static string GetKeyValue(string key)
  39. {
  40. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  41. if (config.AppSettings.Settings[key] == null)
  42. return "";
  43. else
  44. return config.AppSettings.Settings[key].Value;
  45. }
  46. }

猜你在找的XML相关文章