本文主要给大家介绍了关于ASP.NET中Config读写示例的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍吧。
方法如下:
如果是WinForm程序,需要添加引用:
App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="testkey" value="0"></add> </appSettings> </configuration>
NetUtilityLib
using System.Configuration; namespace pcauto { public static class ConfigHelper { ///<summary> ///返回*.exe.config文件中appSettings配置节的value项 ///</summary> ///<param name="strKey"></param> ///<returns></returns> 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; } ///<summary> ///在*.exe.config文件中appSettings配置节增加一对键值对 ///</summary> ///<param name="newKey"></param> ///<param name="newValue"></param> 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");
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对站长技术的支持
一直坚持使用自研Webkit内核的傲游浏览器,现在终于换内核了!经过几个月的测试,...
C. Balance the Bits C. Balance the Bits 题目大意 给你一个含有0和1字符的字符...
我们在做开发时经常会遇到这种情况: a.swf被添加到网页中, a.swf和html页面同时...
一、Redis在微博的应用场景 Redis在微博内部分布在各个应用场景,比如像现在春晚...
本文实例讲述了PHP swoole的process模块创建和使用子进程操作。分享给大家供大家...
这款产品就不多介绍了,进来看的小伙伴多少有点了解,就直接进入正题,如果有不...
windowsubuntu系统引导修复 0 前言 1 新建ESP分区并修复windows引导 2 Ubuntu系...
一、ES6之前声明变量的方式 在ES5及以前版本的JavaScript采用的是var关键字来声...
在从单体式架构迁移到微服务架构时,数据库通常是事后想法。有些人认为迁移仅涉...
在大多数情况下,你都可以操作数据来修改视图,或者反之。但是还是避免不了偶尔...