C# 如何从以特定名称开头的appsettings键获取所有值,并将其传递给任何数组?

C# 如何从以特定名称开头的appsettings键获取所有值,并将其传递给任何数组?,c#,asp.net,asp.net-mvc-4,web-config,C#,Asp.net,Asp.net Mvc 4,Web Config,在我的web.config文件中 <appSettings> <add key="Service1URL1" value="http://managementService.svc/"/> <add key="Service1URL2" value="http://ManagementsettingsService.svc/HostInstances"/> ....lots of keys like above </appSett

在我的
web.config
文件中

<appSettings>
    <add key="Service1URL1" value="http://managementService.svc/"/>
    <add key="Service1URL2" value="http://ManagementsettingsService.svc/HostInstances"/>
    ....lots of keys like above
</appSettings>

要么我做错了,要么我错过了什么。非常感谢您的帮助。

您正在为每次迭代覆盖数组

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();
列表值=新列表();
foreach(ConfigurationManager.AppSettings中的字符串键)
{
if(键启动带(“Service1URL”))
{
字符串值=ConfigurationManager.AppSettings[键];
增加(价值);
}
}
字符串[]repositoryUrls=values.ToArray();
我会使用一点LINQ:

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

我定义了一个类来保存我感兴趣的变量,并遍历属性,在app.config中查找匹配的内容

然后,我可以根据自己的意愿使用该实例。 想法

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语句中定义了值,因此它不能在if语句之外使用。。。。然后在foreach中定义respositoryUrls,这样每次在几个选项中重新创建它(不是说它可以得到“值”)。您可以在一个单独的键中指定服务URL的数量,如果它们是连续的序列,则使用for以这种方式对它们进行迭代(int i=1;我尝试了它的循环,但没有获取任何值,我认为Startwith不起作用。你认为还有其他方法吗?谢谢你的帮助。嘿,谢谢,我添加了ConfigurationManager.AppSettings.AllKeys,而不是ConfigurationManager.AppSettings,工作正常。谢谢。这不起作用。你不能运行where direct因为它不是IEnumerable,所以需要在AppSettings上重新设置。如果按照用户2133616的建议添加AllKeys,则必须执行OfType或类似的操作
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;
 }