Reputation: 39
I want to read a key from App.config
file in C# how can I do this?
I know I can use this,
string filetype = ConfigurationSettings.AppSettings("filetype");
but I don't want to use a hard-coded key.
The reason is that, in my project there are two libraries with separate app.config files, with separate keys. I want to read the keys through one class only, without hard coding them.
Upvotes: 4
Views: 2300
Reputation: 39
i solved this problem by using configuration object as,
return Instance.GetConfigValue(strLst.AppSettings.Settings.AllKeys.ElementAt(0));
Upvotes: 0
Reputation: 102793
You can create strongly-typed sections in app.config, so that you won't need to use the "magic string" keys such as "filetype" in your example.
See: How to: Create Custom Configuration Sections Using ConfigurationSection on MSDN.
Creating the sections is somewhat involved, but there's an excellent Visual Studio plugin that generates all the code and schema from your design. See: Configuration Section Designer on CodePlex.
Update
Looks like I misunderstood the point of the question. There's no way to use multiple app.config files, or an app.config file that is placed in a class library (as far as I know). At runtime, all that's left of the class library is its DLL; by default any app.config file does not get copied over to the application by the msbuild process.
You may want to consider using your own XML configuration file within each class library, and setting its properties:
Build Action
to Content
Copy to Output Directory
to Copy Always
Upvotes: 6