Я пытаюсь использовать раздел конфигурации и элемент конфигурации. Не получить значение, только получить ключ. У меня app.config вот так:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="ProductSettings">
<section name="DellSettings" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<ProductSettings>
<DellSettings>
<add key = "ProductNumber" value="20001" ></add>
<add key =" ProductName" value="Dell Inspiron"></add>
<add key ="Color" value="Black"></add>
<add key =" Warranty" value ="2 Years" ></add>
</DellSettings>
</ProductSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
Класс настройки продукта
вот так:
public class ProductSettings : ConfigurationSection
{
[ConfigurationProperty("DellSettings", IsRequired = true)]
public DellFeatures DellFeatures
{
get
{
return (DellFeatures)this["DellSettings"];
}
}
}
DellFeaturesClass
как этот, который анализирует каждый элемент в конфигурации
public class DellFeatures : ConfigurationElement
{
[ConfigurationProperty("ProductNumber", IsRequired = true)]
public int ProductNumber
{
get
{
return (int)this["ProductNumber"];
}
}
[ConfigurationProperty("ProductName", IsRequired = true)]
public string ProductName
{
get { return (string)this[nameof(ProductName)]; }
set { this[nameof(ProductName)] = value; }
}
[ConfigurationProperty("Color", IsRequired = false)]
public string Color
{
get
{
return (string)this["Color"];
}
}
[ConfigurationProperty("Warranty", IsRequired = false)]
public string Warranty
{
get
{
return (string)this["Warranty"];
}
}
}
Код основного класса выглядит так:
static void Main(string[] args)
{
var productSettings =
ConfigurationManager.GetSection("ProductSettings/DellSettings") as NameValueCollection;
if (productSettings == null)
{
Console.WriteLine("Product Settings are not defined");
}
}
Я вижу только ключи не Значение. Где я делаю не так.