To achieve this, we can use the following namespace: "System.Configuration". Here is a custom code snippet, where the single parameter is the sectionName. sectionName parameter will hold the section name of a configuration file which you want as key-value pair.
- public static Dictionary < string, string > GetConfigFileAsDict(string sectionName) {
- Dictionary < string, string > resultDictionary = new Dictionary < string, string > ();
- try {
- Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (configuration.GetSection(sectionName) is ConfigFileSection section) {
- foreach(ConfigFileElement element in section.Settings) {
- if (element.Value != null && element.Value != "")
- resultDictionary.Add(element.Key, element.Value);
- }
- }
-
- } catch (Exception e) {
-
- }
- return resultDictionary;
- }