Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

c# - App.config problem

i am having a classlibrary with app1.config and also a windows application with app2.config;i am adding the reference of classlibrary in windows application as well as app1.config.is it possible if i call the method class lib it will go to app1.config otherwise it will use app2.config;

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The best you can achieve is to have two separate configuration files, then have the code of one method read the "main" config file (using the ordinary ConfigurationManager.AppSetting[""] code) and other method read the configuration file of the class library using such code:

Configuration config = ConfigurationManager.OpenExeConfiguration(dllFilePath);
KeyValueConfigurationElement element = config.AppSettings.Settings[appSettingKey];
string value = element.Value;

This will read application setting from the config file of DLL sitting in the location of dllFilePath.

You can also have separate section for the class library in the "main" configuration file if relevant I can give sample as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...