相当于一个库'的app.config'(DLL)的 [英] Equivalent to 'app.config' for a library (DLL)
问题描述
有一个相当于的app.config
为库(DLL)?如果没有,什么是存储特定于库的配置设置最简单的方法?请考虑该库可能在不同的应用中使用。
Is there an equivalent to app.config
for libraries (DLLs)? If not, what is the easiest way to store configuration settings that are specific to a library? Please consider that the library might be used in different applications.
推荐答案
您的可以有单独的配置文件,但你必须要读手动时, ConfigurationManager.AppSettings [钥匙]
将只读正在运行的组件的配置。
You can have separate configuration file, but you'll have to read it "manually", the ConfigurationManager.AppSettings["key"]
will read only the config of the running assembly.
首先,在VS项目右键 - >添加 - >新建项目 - >应用程序配置文件
First, in the VS project right click --> Add --> New item --> Application Configuration File
这将增加的App.config
到项目文件夹,把你的设置中有在<的appSettings>
部分。
This will add App.config
to the project folder, put your settings in there under <appSettings>
section.
现在从这个文件中读取具有这样的功能:
Now to read from this file have such function:
string GetAppSetting(Configuration config, string key)
{
KeyValueConfigurationElement element = config.AppSettings.Settings[key];
if (element != null)
{
string value = element.Value;
if (!string.IsNullOrEmpty(value))
return value;
}
return string.Empty;
}
和使用它:
Configuration config = null;
string exeConfigPath = this.GetType().Assembly.Location;
try
{
config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
}
catch (Exception ex)
{
//handle errror here.. means DLL has no sattelite configuration file.
}
if (config != null)
{
string myValue = GetAppSetting(config, "myKey");
...
}
您必须添加引用System.Configuration当然。
You'll have to add reference to System.Configuration of course.
在建项目中,除了你必须在DLL DllName.dll.config
文件,以及,那就是你有DLL本身发布的文件
When building the project, in addition to the DLL you'll have DllName.dll.config
file as well, that's the file you have to publish with the DLL itself.
这篇关于相当于一个库'的app.config'(DLL)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!