lucycopp lucycopp - 2 months ago 6x
C# Question

Unable to locate a number found in a text file c#

I have an application which an ST-LINK programs firmware too and then a report is made on the buffer size, data bits etc.

I have a text file which stores these results. However, there may be a lot of circuit boards in one batch and each time they need to increase there product number by one. So what I want to do is get the program to look at the last board report, see if it has the same batch number (as batches will all be done at once), and if it does increase the product number by 1. If it doesn't have the same batch number then it must be a new batch and the product number will be 1.

At the moment the product number is not updating. Every time it is returning 1.
Here is my code:

public int previousNumber()
int pNumber = 0;
string line; //set string
int counter = 0; //create int
int numberOfLines = File.ReadLines("report.txt").Count();

System.IO.StreamReader file = new System.IO.StreamReader("report.txt"); //create streamreader

while ((line = file.ReadLine()) != null) //until no empty lines
string[] allLines = File.ReadAllLines("report.txt"); //read in report file

if (allLines[numberOfLines - 9] == batchNumberTextBox.Text)
pNumber = int.Parse(allLines[numberOfLines - 7]);
pNumber = 0;

pNumber = pNumber + 1;
return pNumber;

private void saveReport()

int number = previousNumber();
BatchNumber = batchNumberTextBox.Text;
SerialNumber = serialNumberTextBox.Text;
ProductNumber = number;
string ProductNumberString = ProductNumber.ToString();
string inDate = DateTime.Now.ToString("f",
CultureInfo.CreateSpecificCulture("en-UK")); //set date in that format

board newBoard = new board(BatchNumber, SerialNumber, ProductNumberString, BufferSize, StopBits, Parity, DataBits, baudRate, inDate);
File.AppendAllText("batches.txt", "BATCH NUMBER: " + BatchNumber + " - DATE: " + inDate + Environment.NewLine);
System.Windows.MessageBox.Show("Report Saved");
System.Windows.MessageBox.Show("Save failed"); //tell user save failed


And here is the text file for the reports:

enter image description here

Can you see anywhere why it may not be working? I feel like I may have gone a weird way about this so if you can think of a better way it would be much appreciated!

Thank you in advance,


I take it, the batch number is 1234 in your data sample. If so, it's offset from the last line is -10 and not -9. This is the primary source of your error.

There is also a lot of redundancy in your code: you read the whole file several times while reading it once and storing all its lines in an array would be pretty enough.

A simplified (but still correct) version of previousNumber() may look like this:

public int previousNumber()
    var allLines = File.ReadAllLines("report.txt");
    int pNumber = 0;
    if (allLines.Length > 10 && allLines[allLines.Length - 10] == batchNumberTextBox.Text)
        // Note: if the desired value is "1" and not "4096", then the offset is "-8".
        int.TryParse(allLines[allLines.Length - 8], out pNumber);
    return pNumber + 1;