Bohn
Bohn

Reputation: 26919

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?

Upvotes: 3

Views: 74

Answers (1)

Steve Cooper
Steve Cooper

Reputation: 21480

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;

Upvotes: 6

Related Questions