开发者

Reading an app.config in class library

开发者 https://www.devze.com 2023-01-27 10:41 出处:网络
I have a class library and I would like to have some of the configurations stored in an App.config, which would be placed in the same project. I tried reading the configuration using:

I have a class library and I would like to have some of the configurations stored in an App.config, which would be placed in the same project. I tried reading the configuration using:

ConfigurationManager.AppSettings["ABC"];

and it doesnt fetch me any value.

It would be great if you can let me开发者_运维百科 know how I can read my App.config. I don't want to do this using xml. Would be great if you can post come code samples in C#.

Thanks in advance


App.config can only be associated to an executable. Talking about an app.config in a class library makes no sense. So you could use ConfigurationManager.AppSettings["ABC"]; in your class library but you will need to store the values in the app/web.config of the application that will use this class library.


If you have custom code, then add a custom configuration section to your app.config (or web.config) for your executing program.

If using NUnit, name your app.config file with the same name as your *.nunit project filename. So for example, if you called your project "ClassLibraryA.nunit", then name your class library configuration file "ClassLibraryA.config". They also need to reside in the same folder/directory. NUnit is actually using this as the main configuration file

.... add a reference to System.Configuration (in .NET tab)

.... and use this code:

string settingValue = ConfigurationManager.AppSettings["settingName"];
0

精彩评论

暂无评论...
验证码 换一张
取 消