简体   繁体   中英

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!

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

The core problem is that you need to know what the last written transaction ID is, for that you'll need to read in the file you're writing to and parse the last ID written, if none are present (or none were parsed) default to starting from 0 (or 1), something like this:

// Default to 0
var transactionId = 0;
var path = @"C:\MyFolder\MyFile.csv";

// Note ReadLines **not** ReadAllLines
var file = File.ReadLines(path);
var lastWrite = file.Last();

// Try and read the first part of the last line, if it fails just use the default
if (int.TryParse(lastWrite.Split(';')[0], out var lastWrittenId))
    transactionId = lastWrittenId;

var sb = new StringBuilder();
var toWrite = new List<string>();
for (int i = 0; i < 10; i++)
{
    var currentId = transactionId + i;
    var date1 = DateTime.Today.ToString("yyyyMMdd");

    // ...
    
    // Without this check there'd be an empty line at the start of the file
    if (currentId != 0)
        sb.Append(Environment.NewLine);
 
    var logText = $"{currentId};{date1};{...}";
    sb.Append(logText);    
    toWrite.Add(sb.ToString());
    sb.Clear();
}

File.AppendAllLines

FYI: You're (as far as I can tell) currently not appending a new line when writing. I also recommend that you prepend the new line, meaning the last line should always contain the last written entry, this makes parsing the last written entry easier and faster*


*Faster because we don't have to call Reverse() on the supplied IEnumerable , we want to avoid this because Reverse() captures the source into an array and as such will stream the entire file into our memory, while using Last() "simply" iterates over the entire thing and returns the last item , meaning we're not buffering the entire file in memory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM