资讯

精准传达 • 有效沟通

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

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

这篇文章主要介绍ASP.NET中Config文件读写的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

成都创新互联公司是一家集网站建设,梅列企业网站建设,梅列品牌网站建设,网站定制,梅列网站建设报价,网络营销,网络优化,梅列网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

方法如下:

如果是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文件读写的示例分析-创新互联
网页网址:http://cdkjz.cn/article/dcjgod.html
多年建站经验

多一份参考,总有益处

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

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

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