在 WebConfigEditor 中读取和写入 appsettings 的密钥 [英] Reading and Writing keys to appsettings in WebConfigEditor
问题描述
我的项目包含 web.config 文件和一个外部 appSettings 文件.我正在制作一个 WebConfig 编辑器,它具有从 web.config 和外部 appSetting 文件读取 AppSettings 键的选项,以在 webPage 上显示它们.此外,我允许用户通过单击删除"按钮来删除任何键.此外,用户还可以通过单击更新按钮来更新任何键的值.或者他还可以通过单击添加新密钥"按钮来插入新密钥.
My project contains web.config file and an external appSettings file. I am making a WebConfig Editor that has options to Read AppSettings key from web.config and external appSetting file to display them on webPage. Also, I am allowing user to delete any key by clicking on Remove button. Moreover, user can also update any key's value by clicking on update button.Or he can also insert new key by clicking on Add New Key button.
我面临的关键问题是,每当我尝试添加新密钥时,它就会被插入web.config 文件按预期,但同时它将外部 appSettings 文件中存在的所有键添加到 web.config (这是突然的行为).
The key issue I am facing is that whenfever I try to add a new key , it gets inserted into web.config file as expected , but at the same time it adds all the keys present in external appSettings file into web.config ( which is abrupt behavior).
如何在任何密钥的更新/删除/添加功能上阻止密钥从外部 appSettings 文件迁移到 web.config?
How to stop this migration of keys from external appSettings file to web.config on any key's update / delete/ add function?
推荐答案
为了读取,将外部文件放在根目录下的 Config 文件夹中,然后使用此代码根据从 web.config 或外部读取的键名读取键/值文件.
For reading, put external file in Config folder under root and then use this code to read key/values based on key name it read from web.config or external file.
// get from web.config
String myKey = ConfigurationManager.AppSettings.Get("Key1");
String str += "AppSetting value from web.config:" + myKey;
// get from external AppSetting file
myKey = ConfigurationManager.AppSettings.Get("Key2");
String str2 += "AppSetting value from external AppSetting file:" + myKey;
其中 Key1 在 web.config 中,Key2 在外部配置文件中
where Key1 is in web.config and Key2 in external config file
还有
使用 foreach
循环
foreach (string key in ConfigurationManager.AppSettings)
{
string value = ConfigurationManager.AppSettings[key];
Console.WriteLine("Key: {0}, Value: {1}", key, value);
}
这篇关于在 WebConfigEditor 中读取和写入 appsettings 的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!