我试图在多个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);
}
}
}
答案 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);
}
}
}
}