Search code examples
c#variablesincrementuniversal

Increment a set of value each time my program is run in c#


I have to generate a list of transaction IDs consistently every time I run my code and write them into a log. It looks something like this:

String path = @"C:\MyFolder\MyFile.csv";

for (int i = 1; i < 10; i++)
{
    int transactionId = i;
    string TransactionID = transactionId.ToString();

    string date1 = DateTime.Today.ToString("yyyyMMdd");
    string time1 = DateTime.Now.ToString();

    string appendText = TransactionID + ";" + date1 + ";" + time1;
    Environment.NewLine;
    File.AppendAllText(path, appendText);
}

That will write 10 rows numbered with 1 to 10 every time I run this but I need to consistently update the transaction Ids with each time. So I run this code today rows are numbered 1 - 10, I run this code tomorrow rows should be numbered 11-20. How can I have a universal variable that increments with each run?

Thank you very much in advance!


Solution

  • At the startup of your program, read the last transaction ID from the last line of the CSV file. In case no data exists, you know that you should start counting from 1 or 0