Two things:
1) The code you show writes an unknown string to the file: you assemble a string in
final
but write the content of
sb
:
string final = UNB + UNH + BGM + NAD + EQD + DTM + LOC + CNT + UNT + UNZ;
...
File.Write(sb);
2) There are several ways to do what you want. One - as suggested by CPallini - is to use a StringBuilder, which is always a better idea than string concatenation. StringBuilder is an "expandable string" which means it uses less memory that multiple concatenations (remember that strings are immutable, so each time you add two strings, you produce a third.
This code:
string final = UNB + UNH + BGM + NAD + EQD + DTM + LOC + CNT + UNT + UNZ;
generates a number of intermediate strings, each increasing in length as it goes.
A better solution might be to just use the
File.AppendAllText Method (System.IO) | Microsoft Docs[
^] to add each term when you need:
string final = UNB + UNH + BGM + NAD + EQD + DTM + LOC + CNT + UNT + UNZ;
...
File.AppendAllText("Reden.edi", final);