简体   繁体   中英

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. 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. ( link )

To try in another file, you'll want to get the configuration using ConfigurationManager.OpenExeConfiguration Method (String)

So;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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