Ahsan
Ahsan

Reputation: 658

Settings from "app.config" is not being gotton in class library project

Problem is that my settings from "app.config" is not being gotten in class library project.

Can "app.config" be used in applications only and not in class libraries?

Upvotes: 0

Views: 151

Answers (3)

Andrii Kalytiiuk
Andrii Kalytiiuk

Reputation: 1497

By design of .NET framework when you access Application configuration with default means - .NET runtime supplies you with data from app.config of entry assembly (an EXE file that references your class library).

Application Configuration of class library can be accessed in following way (code should be placed in project of class library - in order for Assembly.GetCallingAssembly().Location to return proper path):

ExeConfigurationFileMap classLibraryConfigurationMap= new ExeConfigurationFileMap();
classLibraryConfigurationMap.ExeConfigFilename = Assembly.GetCallingAssembly().Location + ".config";

Configuration classLibraryConfiguration = ConfigurationManager.OpenMappedExeConfiguration(classLibraryConfigurationMap, ConfigurationUserLevel.None);

Upvotes: 0

Chris Payne
Chris Payne

Reputation: 1710

You should add the config settings to the project that is using the class project. ie If you have a project 'MyProject' that references 'MyClassProject' then the app.config settings should be placed in the 'MyProject' project

Upvotes: 1

betelgeuce
betelgeuce

Reputation: 837

Application configuration files contain settings specific to an application. This file contains configuration settings that the common language runtime reads (such as assembly binding policy, remoting objects, and so on), and settings that the application can read. The config file is optional, if it does not exist environments such as ASP.NET will fall back to the machine.config file stored in the .NET system directories to get machine wide defaults.

Think about it like that. If you class library has some custom settings then it should have a config to deal with it.

Upvotes: 0

Related Questions