App.config文件替换关键字" key"和" value"

时间:2016-12-22 09:54:08

标签: c# app-config

我将尽可能具体。我有上面的配置,我检索并使用它。我想要的是更改" "的关键字到" 汽车 "和" "到" 品牌号 "。

这可能吗?

我尝试在更改关键字名称后检索值,但不使用当前代码来检索键/值。

  <!-- Custom section in App.config file. -->

    <configSections>
    <section
      name="CarMapping"
      type="System.Configuration.AppSettingsSection" />
  </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
    </startup>

  <!-- Mapping the cars to specific brandNumber. -->
  <CarMapping>
    <add key="1" value="A"/>
    <add key="2" value="B"/>
    <add key="3" value="C"/>
    <add key="4" value="A"/>
    <add key="5" value="D" />    
  </CarMapping>

从App.config文件获取键/值并将其存储到Dictionary中的代码。

            // Get the configuration file.
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            // Get the appSettings section.
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("CarMapping");

            // Get the settings collection (key/value pairs).
                foreach (string key in appSettings.Settings.AllKeys)
                {
                    string value = appSettings.Settings[key].Value;
                    string keyValue = appSettings.Settings[key].Key;

                Console.WriteLine(appSettings.Settings[key].Key);

                    MyDectionary.Add(keyValue, value); }        

这就是我想要的。请让我知道检索&#34; car&#34;的代码。和&#34; brandNumber&#34;。提前谢谢。

  <!-- Mapping the cars to specific brandNumber. -->
  <CarMapping>
    <add car="1" brandNumber="A"/>
    <add car="2" brandNumber="B"/>
    <add car="3" brandNumber="C"/>
    <add car="4" brandNumber="A"/>
    <add car="5" brandNumber="D" />    
  </CarMapping>

1 个答案:

答案 0 :(得分:0)

您需要创建自己的专用重载才能实现这一目标。对于您的特定配置文件要求,您需要三个新类:一个用于处理该部分,一个用于处理Collection,另一个用于处理该集合中的元素。

配置部分是配置的顶级容器。它也是Collection容器的两倍。通过返回作为集合的配置属性来覆盖Properties成员。

class CarSection : ConfigurationSection
{
    [ConfigurationProperty("", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(CarCollection),
        AddItemName = "add",
        ClearItemsName = "clear",
        RemoveItemName = "remove")]
    public CarCollection Cars
    {
        get
        {

            return (CarCollection)base[property];
        }
    }

    static ConfigurationProperty  property = new ConfigurationProperty(null, typeof(CarCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            var configurationPropertyCollection = new ConfigurationPropertyCollection();
            configurationPropertyCollection.Add(property);
            return configurationPropertyCollection;
        }
    }
}

收藏

这是配置集合的默认直接实现。它充当新CarElements的工厂。

class CarCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new CarElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        var carElement = element as CarElement;
        if (carElement != null)
        {
            var car = (CarElement)carElement;
            return car.Car;
        }
        return null;
    }
}

的ConfigurationElement

CarElement类能够反序列化新属性car和Brandnumber。我认为这辆车是关键。但这可以改变。

class CarElement: ConfigurationElement
{

    [ConfigurationProperty("car", DefaultValue = "42",
    IsRequired = true, IsKey = true)]
    public string Car { get { return (string)this["car"]; } set { this["car"] = value; } }

    [ConfigurationProperty("brandNumber", DefaultValue = "42",
    IsRequired = true, IsKey = true)]
    public string BrandNumber { get { return (string)this["brandNumber"]; }  set { this["car"] = value; } }
}

如果您想利用这一点,您必须在app.config中进行此更改以使您自己的类发挥作用:

<configSections>
    <section
      name="CarMapping"
      type="ConsoleApplication2.CarSection, ConsoleApplication2" />
</configSections>

确保完全限定类型,因此类型 AND 程序集名称。

使用以下代码读取汽车映射:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Get the appSettings section.
var carSettings = (CarSection)config.GetSection("CarMapping");

//Get the settings collection (key / value pairs).
foreach (CarElement car in carSettings.Cars)
{

    Console.WriteLine("{0} - {1} ", car.BrandNumber, car.Car);
}