ASP.NET中Config文件读写功能的示例分析-创新互联-成都快上网建站

ASP.NET中Config文件读写功能的示例分析-创新互联

这篇文章将为大家详细讲解有关ASP.NET中Config文件读写功能的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

我们提供的服务有:成都做网站、网站建设、外贸营销网站建设、微信公众号开发、网站优化、网站认证、嵊泗ssl等。为成百上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的嵊泗网站制作公司

方法如下:

如果是WinForm程序,需要添加引用:

  • System.ServiceModel

  • System.Configuration

App.config



 
 
 

NetUtilityLib

using System.Configuration;
namespace pcauto
{
 public static class ConfigHelper
 { 
  ///  
  ///返回*.exe.config文件中appSettings配置节的value项  
  ///  
  ///  
  /// 
  public static string GetAppConfig(string strKey)
  {
   string file = System.Windows.Forms.Application.ExecutablePath;
   Configuration config = ConfigurationManager.OpenExeConfiguration(file); 
   foreach (string key in config.AppSettings.Settings.AllKeys) { 
    if (key == strKey) { 
     return config.AppSettings.Settings[strKey].Value.ToString(); 
    } 
   }
   return null;
  }
  ///  
  ///在*.exe.config文件中appSettings配置节增加一对键值对  
  /// 
  /// 
  /// 
  public static void UpdateAppConfig(string newKey, string newValue) { 
   string file = System.Windows.Forms.Application.ExecutablePath;
   Configuration config = ConfigurationManager.OpenExeConfiguration(file); 
   bool exist = false; 
   foreach (string key in config.AppSettings.Settings.AllKeys) { 
    if (key == newKey) { exist = true; } 
   } 
   if (exist) { config.AppSettings.Settings.Remove(newKey); }
   config.AppSettings.Settings.Add(newKey, newValue); 
   config.Save(ConfigurationSaveMode.Modified);
   ConfigurationManager.RefreshSection("appSettings");
  }  
 }
}

读示例

ConfigHelper.GetAppConfig("testkey")

写示例

ConfigHelper.UpdateAppConfig("testkey", "abc");

关于“ASP.NET中Config文件读写功能的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


分享题目:ASP.NET中Config文件读写功能的示例分析-创新互联
URL地址:http://kswjz.com/article/dicpdp.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流