开发者

Creating map from values in app.config

Currently, in my C# .cs file, I have declared a dictionary as such:

Dictionary<string, string> example_map = new Dictionary<string, string>();

option_symbol_map.Add("Key11", "Val1");

option_symbol_map.Add("Key2", "Val2");

option_symbol_map.Add("Key3", "Val3");

I need to move these keys and corresponding values to the app.config file so they can be edited as required.

Whats the best way to implement this?

Should 开发者_开发技巧I do it like this:

<add key="Test1" value="Key1,Key2,Key3"/>

<add key="Test2" value="Val1,Val2,Val3"/>

And then read both keys simultaneously while creating my dictionary?

or should I list it this way:

<add key="test3" value="Key1,Val1,Key2,Val2,Key3,Val3"/>

In this case how would I create my map, selecting every alternate object as key and value?


Instead of AppSettings you can write your own ConfigurationElementCollection class that reads something like this:

<yourSection>
   <yourElements>
       <add key="Key1" value="Val1"/>
       <add key="Key2" value="Val2"/>
       <add key="Key3" value="Val3"/>
    </yourElements>      
</yourSection>

Here is an article that covers the most important parts. If you do this then you do not need to know the keys.


What is the 'TestX' for? Why not just use

<add key="Key1" value="Val1"/>
<add key="Key2" value="Val2"/>
<add key="Key3" value="Val3"/>

which fits perfectly with the 'key/value' definition. They can then be accessed like this:

string val1 = ConfigurationManager.AppSettings["Key1"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜