如何解决:索引超出绑定的 C# 数组、流

how to resolve : index out of bound C# arrays, streams

我得到的索引越界:stream.Read(fileBytes[i], 0, fileBytes[i].Length); 任何帮助,将不胜感激 。谢谢 :)

string[] path = new string[15];
byte[][] fileBytes = new byte[10][];
for (int i = 1; i <= 10; i++) {

 path[i] = @ "C:\Users\t-chkum\Desktop\InputFilesMB\" + i + ".txt ";

 // readind data/* 
 FileStream stream = File.OpenRead(path[i]);
 fileBytes[i] = new byte[stream.Length];

 //  Console.WriteLine(stream.Length);
 stream.Read(fileBytes[i], 0, fileBytes[i].Length);
 stream.Close();
}

您正在从 i=1 迭代到 10(含)。但是,数组的大小是 10,即索引从 09。因此,要么从 0 开始 i,要么使用 i-1 进行索引。另外,使用 try-catch。这将帮助您更好地调试。

否则,代码对我来说工作正常。