0

I have a setting like this in my appSettings.config file

<add key="logFilesLocations" value="[firstfile,secondfile]"/>

Is there any standard way to read these values as arrays directly?

I know I can play with the string after reading the value and split string to get the array, but that is ugly.

I tried this also

public static List<string> LogFilesLocations => (ConfigurationManager.AppSettings.GetValues("logFilesLocations") ?? Array.Empty<string>()).ToList();

But this actually does not return an array.

Viswanatha Swamy
  • 699
  • 1
  • 10
  • 17
Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105
  • No built-in way as long as I know, other than parse the string afterwards. Or using a whole different format with more options, like XML or JSON. – Alejandro Jun 08 '21 at 12:29

1 Answers1

0

Got the solution from this answer

https://stackoverflow.com/a/46057375/804517

So I replaced appSettings.config with appSettings.json and put my setting like this

"logFilesLocations": [
        "C:\\Users\\Desktop\\Logs",
        "C:\\Users\\Desktop\\Logs2"
    ]

and started reading the values like this

public static List<string> LogFilesLocations => _config.GetSection("logFilesLocations").Get<List<string>>();

As suggested in the answer I had to add Microsoft.Extensions.Configuration, Microsoft.Extensions.Configuration.Binder and Microsoft.Extensions.Configuration.Json packages

Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105