Lou*_*hys 144 .net c# dll settings app-config
是否有相当于app.config库(DLL)?如果不是,存储特定于库的配置设置的最简单方法是什么?请考虑该库可能在不同的应用程序中使用.
Sha*_*ard 154
您可以拥有单独的配置文件,但是您必须"手动"读取它,ConfigurationManager.AppSettings["key"]它将只读取正在运行的程序集的配置.
假设您使用Visual Studio作为IDE,可以右键单击所需的项目→添加→新项→应用程序配置文件
这将添加App.config到项目文件夹,将您的设置放在<appSettings>部分下.如果您没有使用Visual Studio并手动添加文件,请确保为其指定名称:DllName.dll.config,否则以下代码将无法正常工作.
现在从这个文件中读取有这样的功能:
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;
}
Run Code Online (Sandbox Code Playgroud)
并使用它:
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");
...
}
Run Code Online (Sandbox Code Playgroud)
您还必须添加对System.Configuration命名空间的引用,以使ConfigurationManager类可用.
在构建项目时,除了DLL之外,您还将拥有DllName.dll.config文件,这是您必须使用DLL本身发布的文件.
以上是基本示例代码,对于那些对全尺寸示例感兴趣的人,请参考其他答案.
Paw*_*anS 28
不幸的是,每个可执行文件只能有一个app.config文件,所以如果你的DLL链接到你的应用程序,他们就没有自己的app.config文件.
解决方案是:
您不需要将App.config文件放在类库的项目中.
您将App.config文件放在引用类库的DLL的应用程序中.
例如,假设我们有一个名为MyClasses.dll的类库,它使用app.config文件,如下所示:
string connect =
ConfigurationSettings.AppSettings["MyClasses.ConnectionString"];
Run Code Online (Sandbox Code Playgroud)
现在,假设我们有一个名为MyApp.exe的Windows应用程序,它引用了MyClasses.dll.它将包含一个App.config,其条目如下:
<appSettings>
<add key="MyClasses.ConnectionString"
value="Connection string body goes here" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
要么
xml文件最适合app.config.根据需要使用xml serialize/deserialize.你可以随心所欲地称呼它.如果您的配置是"静态的"并且不需要更改,您还可以将其作为嵌入式资源添加到项目中.
希望它能给出一些想法
配置文件是应用程序范围的,而不是汇编范围的.因此,您需要将库的配置部分放在使用您的库的每个应用程序的配置文件中.
也就是说,从appSettings类库中的应用程序配置文件(特别是部分)获取配置并不是一个好习惯.如果您的库需要参数,那么它们应该作为方法参数在构造函数,工厂方法等中传递给任何调用库的人.这可以防止调用应用程序意外重用类库所期望的配置条目.
也就是说,XML配置文件非常方便,所以我发现最好的折衷方案是使用自定义配置部分.您可以将库的配置放在XML文件中,该文件由框架自动读取和解析,从而避免潜在的事故.
您可以在MSDN上了解有关自定义配置部分的更多信息,Phil Haack也有一篇很好的文章.
我目前正在为零售软件品牌创建插件,它们实际上是 .net 类库。作为一项要求,每个插件都需要使用配置文件进行配置。经过一番研究和测试,我编写了以下类。它可以完美地完成工作。请注意,在我的案例中,我没有实现本地异常处理,因为我在更高级别捕获异常。
在小数和双精度的情况下,可能需要进行一些调整才能使小数点正确,但它适用于我的 CultureInfo ......
static class Settings
{
static UriBuilder uri = new UriBuilder(Assembly.GetExecutingAssembly().CodeBase);
static Configuration myDllConfig = ConfigurationManager.OpenExeConfiguration(uri.Path);
static AppSettingsSection AppSettings = (AppSettingsSection)myDllConfig.GetSection("appSettings");
static NumberFormatInfo nfi = new NumberFormatInfo()
{
NumberGroupSeparator = "",
CurrencyDecimalSeparator = "."
};
public static T Setting<T>(string name)
{
return (T)Convert.ChangeType(AppSettings.Settings[name].Value, typeof(T), nfi);
}
}
Run Code Online (Sandbox Code Playgroud)
App.Config 文件示例
<add key="Enabled" value="true" />
<add key="ExportPath" value="c:\" />
<add key="Seconds" value="25" />
<add key="Ratio" value="0.14" />
Run Code Online (Sandbox Code Playgroud)
用法:
somebooleanvar = Settings.Setting<bool>("Enabled");
somestringlvar = Settings.Setting<string>("ExportPath");
someintvar = Settings.Setting<int>("Seconds");
somedoublevar = Settings.Setting<double>("Ratio");
Run Code Online (Sandbox Code Playgroud)
归功于 Shadow Wizard 和 MattC
public class ConfigMan
{
#region Members
string _assemblyLocation;
Configuration _configuration;
#endregion Members
#region Constructors
/// <summary>
/// Loads config file settings for libraries that use assembly.dll.config files
/// </summary>
/// <param name="assemblyLocation">The full path or UNC location of the loaded file that contains the manifest.</param>
public ConfigMan(string assemblyLocation)
{
_assemblyLocation = assemblyLocation;
}
#endregion Constructors
#region Properties
Configuration Configuration
{
get
{
if (_configuration == null)
{
try
{
_configuration = ConfigurationManager.OpenExeConfiguration(_assemblyLocation);
}
catch (Exception exception)
{
}
}
return _configuration;
}
}
#endregion Properties
#region Methods
public string GetAppSetting(string key)
{
string result = string.Empty;
if (Configuration != null)
{
KeyValueConfigurationElement keyValueConfigurationElement = Configuration.AppSettings.Settings[key];
if (keyValueConfigurationElement != null)
{
string value = keyValueConfigurationElement.Value;
if (!string.IsNullOrEmpty(value)) result = value;
}
}
return result;
}
#endregion Methods
}
Run Code Online (Sandbox Code Playgroud)
只是为了做某事,我把最重要的答案重构成了一个类.用法如下:
ConfigMan configMan = new ConfigMan(this.GetType().Assembly.Location);
var setting = configMan.GetAppSetting("AppSettingsKey");
Run Code Online (Sandbox Code Playgroud)