Multiple values for a single config key

后端 未结 10 2084
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 01:37

I\'m trying to use ConfigurationManager.AppSettings.GetValues() to retrieve multiple configuration values for a single key, but I\'m always receiving an array o

10条回答
  •  猫巷女王i
    2020-12-05 02:20

    My take on JJS's reply: Config file:

    
    
      
        

    Code to retrieve into string[]

     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;
        }
    

    IMO, that's as simple as it is going to get. Feel free to prove me wrong :)

提交回复
热议问题