Json从多个文件中读取id以显示在文本框中

时间:2018-02-03 12:52:02

标签: c# json

我试图在多个json文件的列表框上显示版本ID,但我得到了 此错误:方法' OpenText'在OpenText上接受2个参数

using (StreamReader file = File.OpenText(@"C:\Users\User\AppData\Roaming\.minecraft\versions\", "*.json"))
{
    JsonSerializer serializer = new JsonSerializer();
    VersionJsonRead MCVersionsList = (VersionJsonRead)serializer.Deserialize(file, typeof(VersionJsonRead));
    foreach (var item in MCVersionsList.id)
    {
        if (!Regex.IsMatch((string)MCVersionsList.id, "[a-z]"))
        {
            versionsList.Items.Add((string)MCVersionsList.id);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

File.OpenText仅用于打开单个文件。如果要打开多个文件,可以使用Directory.GetFiles并使用该结果调用逻辑:

var files = Directory.GetFiles(@"C:\Users\User\AppData\Roaming\.minecraft\versions\", "*.json");

foreach (var filePath in files)
{
    using (StreamReader file = File.OpenText(filePath))
    {
        JsonSerializer serializer = new JsonSerializer();
        VersionJsonRead MCVersionsList = (VersionJsonRead)serializer.Deserialize(file, typeof(VersionJsonRead));
        foreach (var item in MCVersionsList.id)
        {
            if (!Regex.IsMatch((string)MCVersionsList.id, "[a-z]"))
            {
                versionsList.Items.Add((string)MCVersionsList.id);
            }
        }
    }
}