ASP.NET中Config文件的读写示例
本文主要给大家介绍了关于ASP.NET中Config读写示例的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍吧。
方法如下:
如果是WinForm程序,需要添加引用:
- System.ServiceModel
- System.Configuration
App.config
NetUtilityLib
usingSystem.Configuration; namespacepcauto { publicstaticclassConfigHelper { //////返回*.exe.config文件中appSettings配置节的value项 /// ////// publicstaticstringGetAppConfig(stringstrKey) { stringfile=System.Windows.Forms.Application.ExecutablePath; Configurationconfig=ConfigurationManager.OpenExeConfiguration(file); foreach(stringkeyinconfig.AppSettings.Settings.AllKeys){ if(key==strKey){ returnconfig.AppSettings.Settings[strKey].Value.ToString(); } } returnnull; } /// ///在*.exe.config文件中appSettings配置节增加一对键值对 /// ////// publicstaticvoidUpdateAppConfig(stringnewKey,stringnewValue){ stringfile=System.Windows.Forms.Application.ExecutablePath; Configurationconfig=ConfigurationManager.OpenExeConfiguration(file); boolexist=false; foreach(stringkeyinconfig.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");
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持