简体   繁体   English

从app.config文件中读取

[英]Reading from an app.config file

Given an app.config file, How can I read the keys and values given that those keys I am interested in will all follow a pattern I establish first. 给定一个app.config文件,我如何读取键和值,因为我感兴趣的那些键都将遵循我首先建立的模式。 For example I do know that all the names of my keys will follow a pattern of 例如,我知道我的密钥的所有名称都将遵循模式

<add key="my_*_Def"  value = "someValue">

The thing is I want to write a program that I can give these config files to it and then my program goes and finds that pattern in that file and gives them to me for further processing. 问题是我想编写一个程序,我可以将这些配置文件提供给它,然后我的程序去找那个文件中的模式并将它们交给我进行进一步处理。

Is there a better way for writing this other than treating it as a text file and reading it line by line? 除了将其视为文本文件并逐行阅读之外,是否有更好的方法来编写此文件?

You can use 您可以使用

ConfigurationManager.AppSettings.AllKeys

to find all the keys in the app.config file. 找到app.config文件中的所有键。 ( link ) 链接

To try in another file, you'll want to get the configuration using ConfigurationManager.OpenExeConfiguration Method (String) 要尝试其他文件,您需要使用ConfigurationManager.OpenExeConfiguration Method (String)获取配置

So; 所以;

var config = ConfigrationManager.OpenExeConfiguration("foo.exe.config");
var keys = config.AppSettings.AllKeys;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM