我有一个非常恼人的错误。
我的以下代码就是我所拥有的(同样在1小时前工作)
using (StreamReader reader = new StreamReader(dir + fileDAT))
{
string line;
while ((line = reader.ReadLine()) != null)
{
line = reader.ReadLine();
if (line.Substring(0,5) == "\tVNUM\t")
{
vnum = Convert.ToInt32(line.Substring(6));
Console.ReadLine();
Console.WriteLine(line); // Write to console.
}
}
}
但是现在它抛出了if (line.Substring(0,5) == "\tVNUM\t")
行ArgumentOutOfRangeException
你知道如何解决这个问题吗? “\ t”有多少指数?
答案 0 :(得分:1)
line
可能少于5个字符长
将if
替换为以下内容:
if (line.Length >= 5 && line.Substring(0,5) == "\tVNUM\t")
\t
char是一个char,因此它的长度为1
char。
顺便说一下,你确定每次迭代你真的需要读两次线吗?我的意思是以下代码部分:
while ((line = reader.ReadLine()) != null) // 1st read
{
line = reader.ReadLine(); // 2nd read
修改强>
你知道,字符串"\tVNUM\t"
长6
个字符。将它与5个字符长度子串进行比较没有任何意义。