资讯

精准传达 • 有效沟通

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

ASP.NET中Config文件是怎么读写的-创新互联

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

创新互联是一家集成都网站建设、做网站、网站页面设计、网站优化SEO优化为一体的专业网站设计公司,已为成都等多地近百家企业提供网站建设服务。追求良好的浏览体验,以探求精品塑造与理念升华,设计最适合用户的网站页面。 合作只是第一步,服务才是根本,我们始终坚持讲诚信,负责任的原则,为您进行细心、贴心、认真的服务,与众多客户在蓬勃发展的市场环境中,互促共生。

方法如下:

如果是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/goioo.html
多年建站经验

多一份参考,总有益处

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

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

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220