Multiple values for a single config key
我试图使用
1 |
我试着用
1 | ConfigurationManager.AppSettings.GetValues("mykey"); |
但我只得到了
有什么解决办法吗?
尝试
1 |
和
1 | string[] mykey = ConfigurationManager.AppSettings["mykey"].Split(','); |
配置文件将每一行视为一个分配,这就是为什么您只看到最后一行的原因。当它读取配置时,它会给你的键分配值"A",然后是"B",然后是"C",因为"C"是最后一个值,所以它是坚持的值。
正如@kevin建议的那样,最好的方法可能是一个值,该值的内容是一个csv,您可以对其进行分析。
我知道我迟到了,但我发现这个解决方案非常有效,所以我只想和大家分享一下。
一切都是为了定义你自己的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | namespace Configuration.Helpers { public class ValueElement : ConfigurationElement { [ConfigurationProperty("name", IsKey = true, IsRequired = true)] public string Name { get { return (string) this["name"]; } } } public class ValueElementCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new ValueElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((ValueElement)element).Name; } } public class MultipleValuesSection : ConfigurationSection { [ConfigurationProperty("Values")] public ValueElementCollection Values { get { return (ValueElementCollection)this["Values"]; } } } } |
在app.config中,只需使用新的部分:
1 2 3 4 5 6 7 8 9 10 11 | <configSections> <section name="PreRequest" type="Configuration.Helpers.MultipleValuesSection, Configuration.Helpers" requirePermission="false" /> </configSections> <PreRequest> <Values> </Values> </PreRequest> |
当检索这样的数据时:
1 2 3 4 | var section = (MultipleValuesSection) ConfigurationManager.GetSection("PreRequest"); var applications = (from object value in section.Values select ((ValueElement)value).Name) .ToList(); |
最后感谢原帖作者
你想做的是不可能的。您要么对每个键进行不同的命名,要么执行类似于value="a、b、c"的操作,然后在代码
它将始终获取上次定义的键的值(在示例C中)。
我认为,您可以使用自定义配置部分http://www.4guysfromrolla.com/articles/032807-1.aspx
我使用钥匙的命名规则,它的工作方式很有魅力
1 2 3 4 5 6 7 8 9 10 11 | <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="section1" type="System.Configuration.NameValueSectionHandler"/> </configSections> <section1> </section1> </configuration> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var section1 = ConfigurationManager.GetSection("section1") as NameValueCollection; for (int i = 0; i < section1.AllKeys.Length; i++) { //if you define the key is unique then use == operator if (section1.AllKeys[i] =="keyName1") { // process keyName1 } // if you define the key as a list, starting with the same name, then use string StartWith function if (section1.AllKeys[i].Startwith("keyName2")) { // AllKeys start with keyName2 will be processed here } } |
由于
这将匹配像
我接受JJS的回复:配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="List1" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <section name="List2" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <List1> </List1> <List2> </List2> </configuration> |
要检索到字符串[]中的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private void button1_Click(object sender, EventArgs e) { string[] output = CollectFromConfig("List1"); foreach (string key in output) label1.Text += key + Environment.NewLine; label1.Text += Environment.NewLine; output = CollectFromConfig("List2"); foreach (string key in output) label1.Text += key + Environment.NewLine; } private string[] CollectFromConfig(string key) { NameValueCollection keyCollection = (NameValueCollection)ConfigurationManager.GetSection(key); return keyCollection.AllKeys; } |
依我看,这很简单。请随意证明我错了:)
以下是完整的解决方案:ASPX.CS中的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | namespace HelloWorld { public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { UrlRetrieverSection UrlAddresses = (UrlRetrieverSection)ConfigurationManager.GetSection("urlAddresses"); } } public class UrlRetrieverSection : ConfigurationSection { [ConfigurationProperty("", IsDefaultCollection = true,IsRequired =true)] public UrlCollection UrlAddresses { get { return (UrlCollection)this[""]; } set { this[""] = value; } } } public class UrlCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new UrlElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((UrlElement)element).Name; } } public class UrlElement : ConfigurationElement { [ConfigurationProperty("name", IsRequired = true, IsKey = true)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } [ConfigurationProperty("url", IsRequired = true)] public string Url { get { return (string)this["url"]; } set { this["url"] = value; } } } } |
在网络配置中
1 2 3 4 5 6 7 8 | <configSections> <section name="urlAddresses" type="HelloWorld.UrlRetrieverSection" /> </configSections> <urlAddresses> </urlAddresses> |