You can do this:
- Read the file line-by-line.
- For each file:
- Split into two parts by
" "
and take the second part. That gives you the previously saved ChildOrderInput. Note: this will not work of OrderInputText can possibly contain spaces; because, if that is possible, a space is not a clear separator for OrderInputText and ChildOrderInput.Text. So if OrderInputText can contain spaces, use another separator than " "
. - If that saved ChildOrderInput equals the current one, break the loop and show the error.
(As a side note, I don't see the point of
"\n" + Environment.NewLine
- if you want two newlines, you better use
Environment.NewLine + Environment.NewLine
)
bool displayError = false;
using (StreamReader sr = new StreamReader("textSA.txt"))
{
while (sr.Peek() > -1)
{
string line = sr.ReadLine();
if (!string.IsNullOrWhiteSpace(line))
{
string parts = line.Split(new char[] { ' ' }, 2);
if (parts.Length < 2) { }
string previousChildOrderInput = parts[1];
if (previousChildOrderInput.Equals(ChildOrderInput.Text))
{
displayError = true;
break;
}
}
}
}
if (displayError)
{
}