相当于库 (DLL) 的“app.config" [英] Equivalent to 'app.config' for a library (DLL)

查看:27
本文介绍了相当于库 (DLL) 的“app.config"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有与 app.config 等效的库 (DLL)?如果没有,存储特定于库的配置设置的最简单方法是什么?请考虑该库可能用于不同的应用程序.

解决方案

可以拥有单独的配置文件,但您必须手动"阅读它,ConfigurationManager.AppSettings["key"] 将只读取正在运行的程序集的配置.

假设您使用 Visual Studio 作为 IDE,您可以右键单击所需的项目 → 添加 → 新建项目 → 应用程序配置文件

这会将 App.config 添加到项目文件夹中,将您的设置放在 部分下.如果您不使用 Visual Studio 并手动添加文件,请确保为其命名:DllName.dll.config,否则以下代码将无法正常工作.

现在读取这个文件有这样的功能:

string GetAppSetting(Configuration config, string key){KeyValueConfigurationElement 元素 = config.AppSettings.Settings[key];如果(元素!= null){字符串值 = element.Value;if (!string.IsNullOrEmpty(value))返回值;}返回字符串.空;}

并使用它:

配置 config = null;字符串 exeConfigPath = this.GetType().Assembly.Location;尝试{config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);}捕获(异常前){//handle errror here..表示DLL没有卫星配置文件.}如果(配置!= null){string myValue = GetAppSetting(config, "myKey");...}

您还必须添加对 System.Configuration 命名空间的引用,以使 ConfigurationManager 类可用.

在构建项目时,除了 DLL 之外,您还将拥有 DllName.dll.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.

解决方案

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.

Assuming you're using Visual Studio as your IDE, you can right click the desired project → Add → New item → Application Configuration File

This will add App.config to the project folder, put your settings in there under <appSettings> section. In case you're not using Visual Studio and adding the file manually, make sure to give it such name: DllName.dll.config, otherwise the below code won't work properly.

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;
}

And to use it:

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");
    ...
}

You'll also have to add reference to System.Configuration namespace in order to have the ConfigurationManager class available.

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.

The above is basic sample code, for those interested in a full scale example, please refer to this other answer.

这篇关于相当于库 (DLL) 的“app.config"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆