【发布时间】: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