Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从web.config应用程序键获取列表中的值_C# - Fatal编程技术网

C# 如何从web.config应用程序键获取列表中的值

C# 如何从web.config应用程序键获取列表中的值,c#,C#,我的web.config中有以下内容: <add key="someValuestoReturn" value="60,59,58,57,56"/> Private int GetValues(){ var _list = System.Configuration.ConfigurationManager.AppSettings["someValuestoReturn"].ToList(); //<< doesn't work return _li

我的web.config中有以下内容:

<add key="someValuestoReturn" value="60,59,58,57,56"/>
    Private int GetValues(){

    var _list = System.Configuration.ConfigurationManager.AppSettings["someValuestoReturn"].ToList();  //<< doesn't work

return _list
    }
我想将此整数值的列表返回到我的方法中,但我很难从web.config调用值列表:

<add key="someValuestoReturn" value="60,59,58,57,56"/>
    Private int GetValues(){

    var _list = System.Configuration.ConfigurationManager.AppSettings["someValuestoReturn"].ToList();  //<< doesn't work

return _list
    }
实现这一目标的最佳方式是什么


谢谢

返回的是字符串而不是列表

对于列表使用:


它将返回一个字符串数组

我建议您用另一种方法解决这个问题

让我们首先创建一个空字符串列表,稍后将其放入数组:

在这之后,我们需要找到您的密钥,并需要将密钥中的值放入新创建的列表中,但我们必须注意拆分,因为您的值是逗号分隔的,所以请检查下面的代码:

List<string> values = new List<string>();

foreach (string key in ConfigurationManager.AppSettings)
        {
            if (key.StartsWith("someValuestoReturn"))
            {
                string value = ConfigurationManager.AppSettings[key].Split(',');
                values.Add(value);
            }

        }

string[] myValuesfromWebConfig = values.ToArray();
因为您的值是逗号分隔的,我猜您希望将它们分别添加到数组/列表中..:

谢谢

编辑:

由于您在注释中写入的错误,我们可以跳过将其添加到数组中,只需删除该行,您的值仍将存储在列表值中,因此最后它可能会如下所示:

List<string> values = new List<string>();

foreach (string key in ConfigurationManager.AppSettings)
{
     if (key.StartsWith("someValuestoReturn"))
     {
                    string value = ConfigurationManager.AppSettings[key].Split(',');
                    values.Add(value);
     }

}

值以逗号分隔;读入字符串并用逗号分开。嗨,谢谢。。我得到了以下错误:无法将类型“string”转换为“string[]”知道为什么吗?@1未来到底在哪一行?可能在这里:string[]myValuesfromWebConfig=values.ToArray;查看我的EDIT@1future如果这个答案对您有帮助,请接受并投票:谢谢这一行:string value=ConfigurationManager.AppSettings[key].Split','@1未来如果这个答案对你有帮助,请投赞成票:
List<string> values = new List<string>();

foreach (string key in ConfigurationManager.AppSettings)
{
     if (key.StartsWith("someValuestoReturn"))
     {
                    string value = ConfigurationManager.AppSettings[key].Split(',');
                    values.Add(value);
     }

}
foreach(var item in values)
{
  //Do whatever you want with your item
}