I am reading values from a selected textfile and then displaying them in a textbox present on my Windows.Form. After my whole textfile has been looped through, the last line in my textfile is repeated again and then I receive the error message: "Exception: Index out of array range."
Do you know what I could have done wrong in my loop?
Amazing, I took away the two blank lines at the end of my file and it worked! How do I prevent this from happening though without manually taking away the blank lines?
while ((line = mStreamReader.ReadLine()) != null)
{
SplitLine(line);
ConvertToLong();
}
into
Code:
while ((line = mStreamReader.ReadLine()) != null)
{
if (string.IsNullOrEmpty(line))
break;
SplitLine(line);
ConvertToLong();
}
But this will also break the operation if there is an blank line in the middle of the file. You could also ignore blank lines
Code:
while ((line = mStreamReader.ReadLine()) != null)
{
if (!string.IsNullOrEmpty(line)) //only execute if line is not empty
{
SplitLine(line);
ConvertToLong();
}
}
Bookmarks