[英]How to get keys and values from Appsettings of Config file in Linq
I am trying to get all the keys and values of config file's app settings in a page constructor so I can use them in the page. 我试图在页面构造函数中获取配置文件的应用程序设置的所有键和值,以便我可以在页面中使用它们。 I tried Linq but I am not sure how to get values along with keys in a simple manner.
我尝试过Linq,但我不确定如何以简单的方式获取值和键。 right now I got all keys and then using foreach to get all values and I am sure that is not a smart way.
现在我得到了所有键,然后使用foreach获取所有值,我确信这不是一个聪明的方法。 Please advice..
请指教..
string[] repositoryUrls = ConfigurationManager.AppSettings.AllKeys
.Select(key => ConfigurationManager.AppSettings[key])
.ToArray();
Thanks 谢谢
It sounds like you actually want a dictionary of Key names and Value values instead of a string array. 听起来你真的想要一个键名和值值的字典而不是字符串数组。
var dict =
ConfigurationManager.AppSettings.AllKeys
.ToDictionary(k => k, v => ConfigurationManager.AppSettings[v]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.