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");
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下