Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all the values from appsettings key which starts with specific name and pass this to any array?

I'd use a little LINQ:

string[] repositoryUrls = ConfigurationManager.AppSettings.AllKeys
                             .Where(key => key.StartsWith("Service1URL"))
                             .Select(key => ConfigurationManager.AppSettings[key])
                             .ToArray();

You are overwriting the array for every iteration

List<string> values = new List<string>();
foreach (string key in ConfigurationManager.AppSettings)
        {
            if (key.StartsWith("Service1URL"))
            {
                string value = ConfigurationManager.AppSettings[key];
                values.Add(value);
            }

        }

string[] repositoryUrls = values.ToArray();

I defined a class to hold the variables I am interested in and iterate through the properties and look for something in the app.config to match.

Then I can consume the instance as I wish. Thoughts?

public static ConfigurationSettings SetConfigurationSettings
{
    ConfigurationSettings configurationsettings = new   ConfigurationSettings();
    {
        foreach (var prop in  configurationsettings.GetType().GetProperties())
        {
            string property = (prop.Name.ToString());
            string value = ConfigurationManager.AppSettings[property];
            PropertyInfo propertyInfo = configurationsettings.GetType().GetProperty(prop.Name);
            propertyInfo.SetValue(configurationsettings, value, null);
        }
    }

    return configurationsettings;
 }