资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

ASP.NET中Config文件是怎么读写的

小编给大家分享一下ASP.NET中Config文件是怎么读写的,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联建站-专业网站定制、快速模板网站建设、高性价比海城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式海城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖海城地区。费用合理售后完善,十年实体公司更值得信赖。

方法如下:

如果是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://cdkjz.cn/article/jjgssi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220