int count = 0;
private void button1_Click(object sender, EventArgs e)
{
List<string> files = new DirectoryInfo(@"d:\")
.GetFiles("*.avi")
.Select(f => f.FullName + " " + FileSizeDisplay(f,70))//Select(f => Path.GetFileName(f.Name) + " " + FileSizeDisplay(f,70))
.ToList();
for (int i = files.Count -1; i >= 0; i--)
{
if (!files[i].EndsWith("MB") || files[i].EndsWith("0 MB"))
{
files.RemoveAt(i);
}
}
if (count < files.Count -1)
{
int index = files[count].IndexOf(" ");
filetoupload = files[count].Substring(0, index);
count += 1;
}
}
在这一行:
if (count < files.Count -1)
我在文件中有13个项目。文件是List<string>
。
如果我只在(count < files.Count)
时才会这样做,当它到达13时会抛出异常。
如果我正在if (count < files.Count -1)
,那么它会显示我点击/上传了12次/项目。
答案 0 :(得分:1)
执行此操作时:
files.RemoveAt(i);
您已更改数组中的项目数。因此,当您尝试迭代该数组时,您应该期望它可能少于13个项目。