简体   繁体   English

如何从Linq中的Config文件的Appsettings中获取键和值

[英]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.

相关问题 如何从 AppSettings.Config 文件中获取键值? - How to get the key value from the AppSettings.Config file? AppSettings 从 .config 文件获取值 - AppSettings get value from .config file 如何找回非 <appSettings> 从web.config的密钥? - How to retrieve non <appSettings> keys from the web.config? 如何在 DI 设置期间自动验证 appSettings.json 文件中的配置值? - How to validate config values from appSettings.json file automatically during DI setup? 如何使用c#winform从appSettings中获取给定配置文件的值? - how to get value from appSettings for given config file using c# winform? 如何从程序集配置文件中检索AppSettings? - How do I retrieve AppSettings from the assembly config file? 如何从多个位置标签中有多个appSettings标签的配置文件中读取appSettings? - How to read appSettings from a config file where there are multiple appSettings tag inside multiple location tag? 从 appsettings.json 配置文件中读取 - Read from appsettings.json config file 在独立的 .NET Core DLL 中,如何从 appsettings.json 配置文件中获取值? - In a stand-alone .NET Core DLL, how do I get a value from the appsettings.json config file? C# 如何从 appsettings.json 获取值 - C# how to get values from appsettings.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM