索引越界数组异常+ Streamreader

时间:2011-09-28 02:59:10

标签: c#

我遇到了基于以下代码的数组索引超出范围异常:

我知道readtoend方法可能会破坏streamreader的指针,因此我实现了discardbuffereddata和seekorigin 0.但是,我仍然得到一个超出范围的索引。请帮忙?谢谢!

StreamReader sr = new StreamReader(@".\Input\" + inServer + ".txt");
        readall = sr.ReadToEnd();
        string[] splited = Regex.Split(readall, "~~~~~~~~~~~~~~");
        sr.BaseStream.Seek(0, SeekOrigin.Begin);
        sr.DiscardBufferedData();


    while (sr.Peek() >= 0)
            {
                currLine = sr.ReadLine();
}

1 个答案:

答案 0 :(得分:0)

尝试修改您的代码,如下所示:

        readAll = sr.ReadToEnd();
        sr.Close();
        foreach (string s in readAll.Split(new char[] { '\r', 'n' }))
        {

            currLine = s;