【问题标题】:How to add a dll config file to a project that add the dll as reference?如何将 dll 配置文件添加到添加 dll 作为参考的项目中?
【发布时间】:2016-08-01 15:18:15
【问题描述】:

我写了一个 UserLogin dll。 该 dll 使用带有一些默认参数的 App.Config 文件。我已将配置文件重命名为 UserLogin.Config 以获得更好的文件系统可读性。

我编写了一个需要用户登录的新应用程序。我添加了 UserLogin.dll 作为对新应用程序的引用。问题是配置文件没有添加参考。我必须手动将它添加到 bin\debug 文件夹才能通过 Visual Studio 运行应用程序。

我在部署它时没有这样的问题,因为我将配置文件添加为设置的一部分。

将配置文件添加到新应用程序以便我可以在开发环境中运行它的正确方法是什么?

以下是 UserLogin.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="IsTestEnvironment" value="false" />
        <add key="AllowedUserForTestEnvironment" value="ehh" />
        <add key="EnableLogging" value="false" />
     </appSettings>
</configuration>

从 UserLogin.config 文件中读取的类:

public static class ConfigurationReader
{
    private static AppSettingsSection _appSettings;

    public static AppSettingsSection AppSettings
    {
        get
        {
            if (_appSettings == null)
            {
                var userPreferencesConfigFileMap = new ExeConfigurationFileMap();
                userPreferencesConfigFileMap.ExeConfigFilename = "UserLogin.config";
                var userPreferencesConfig = ConfigurationManager.OpenMappedExeConfiguration(userPreferencesConfigFileMap, ConfigurationUserLevel.None);
                _appSettings = (AppSettingsSection)userPreferencesConfig.GetSection("appSettings");                                        
            }

            return _appSettings;
        }
    }

    public static bool IsTestEnvironment
    {
        get
        {
            return bool.Parse(AppSettings.Settings["IsTestEnvironment"].Value);
        }
    }

    public static string AllowedUserForTestEnvironment
    {
        get
        {
            return AppSettings.Settings["AllowedUserForTestEnvironment"].Value;
        }
    }

    public static string EnableLogging
    {
        get
        {
            return AppSettings.Settings["EnableLogging"].Value;
        }
    }
}

使用 UserLogin.dll 的新应用程序:

【问题讨论】:

  • 设置有限制,它们只对 EXE 有效。如果您考虑一下,这是有道理的,一个库可以在多个程序中使用并且具有不同的设置。从技术上讲,您必须将 app.config 条目手动合并到 EXE 项目的 app.config 中。不是很有趣。一个正确的方法是让一个库由 EXE 中的代码配置。或者只使用您自己从众所周知的位置加载的 .xml 文件。
  • 感谢您的解释

标签: c# .net dll deployment app-config


【解决方案1】:

问题是您已将配置文件名重命名为自定义名称。

理想情况下,任何 DLL 都可以使用来自父级 Exes App.config 的配置,您只需将您的 appsettings 添加到使用该 dll 的应用程序(exe)的 App.config 文件中。

为什么要这样分开。这不是一个好习惯。

现在既然你把它分开了,它就像拥有一个自定义的 xml 配置文件一样。

在这种情况下,您可以尝试添加 Post-Build 或 Pre-Build 命令,使用 XCOPY 将配置文件复制到正确的输出目录,以便 DLL 拾取它。

右键项目属性,进入 Pre-Build 事件,添加类似命令

xcopy /y "$(SolutionDir)MyProject\myFile.xxx" "$(OutputDir)"

请注意,您可能需要更改路径和文件名以满足您的需要。

【讨论】:

  • 如果我理解你的话,你是说最好把所有的东西都写到dll的App.Config中。对于每个将使用此 dll 的应用程序,我都必须将 AppSettings 部分复制到 Application AppConfig 文件中,对吧?
  • 是的,你没看错。只需取消自定义文件,以及 exes app.config 中的应用设置,然后正常的配置 API 将照常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多