using System;
using System.Configuration;
using System.IO;
using System.Reflection;
namespace Config.Net.Stores
{
///
/// Reads configuration from the .dll.config or .exe.config file.
///
class AssemblyConfigStore : IConfigStore
{
private readonly Configuration _configuration;
///
/// Creates a new instance of assembly configuration store (.dll.config files)
///
/// reference to the assembly to look for
public AssemblyConfigStore(Assembly assembly)
{
_configuration = ConfigurationManager.OpenExeConfiguration(assembly.Location);
}
///
/// Store name
///
public string Name => Path.GetFileName(_configuration.FilePath);
///
/// Store is readable
///
public bool CanRead => true;
///
/// Store is not writeable
///
public bool CanWrite => false;
///
/// Reads the value by key
///
public string? Read(string key)
{
KeyValueConfigurationElement element = _configuration.AppSettings.Settings[key];
return element?.Value;
}
///
/// Writing is not supported
///
public void Write(string key, string? value) => throw new NotSupportedException();
///
/// Nothing to dispose
///
public void Dispose()
{
}
}
}