How to get a List collection of values from app.config in WPF?

前端 未结 7 1132
执笔经年
执笔经年 2020-11-28 22:42

The following example fills the ItemsControl with a List of BackupDirectories which I get from code.

How can I change this

7条回答
  •  囚心锁ツ
    2020-11-28 23:31

    Had the same problem, but solved it in a different way. It might not be the best solution, but its a solution.

    in app.config:

    
    
    

    Then in my configuration wrapper class, I add a method to search keys.

            public List SearchKeys(string searchTerm)
            {
                var keys = ConfigurationManager.AppSettings.Keys;
                return keys.Cast()
                           .Where(key => key.ToString().ToLower()
                           .Contains(searchTerm.ToLower()))
                           .Select(key => ConfigurationManager.AppSettings.Get(key.ToString())).ToList();
            }
    
    
    

    For anyone reading this, i agree that creating your own custom configuration section is cleaner, and more secure, but for small projects, where you need something quick, this might solve it.

    提交回复
    热议问题