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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With