从 3 个不同的 RichTextBoxes 条目构建一个 CSV 文件

Build a CSV file out of 3 different RichTextBoxes entries

我正在努力寻找最佳方法或正确的数据结构来解决 C# 中的以下问题 Visual Studio:

我有 3 个 RichTextBoxes

RichTextBoxes

每个人都会接受名字、姓氏和国家/地区等数据

我将获取每个 RichTextBox 的每个第一个条目,并使用 StringBuilder 将它们放在同一行中,然后循环到下一个和下一个直到结束

它应该看起来像

名字 1,姓氏 1,国家 1

名字 2,姓氏 2,国家 2

等等

            excelFile = new ExcelFile();
            var date = DateTime.Today.ToString("yyyy-MM-dd");
            var csv = new StringBuilder();

            var newLine = "FirstName, LastName, Country";
            csv.AppendLine(newLine);

            foreach (var name in rtextbox_firstname.Lines)
            {
              need to loop through each rich text box then add all to same data set (which not sure which would be good in my case) then loop thru to create the file
            }

            var path = "C:\Users\Shahi\Desktop\" + "run this " + "_" + date + ".csv";
            File.AppendAllText(path, csv.ToString());

为什么不能简单地使用 for 循环来代替 foreach,

excelFile = new ExcelFile();
var date = DateTime.Today.ToString("yyyy-MM-dd");
var csv = new StringBuilder();

var newLine = "FirstName, LastName, Country";
csv.AppendLine(newLine);

//You can update exit condition if there are different number of entries in richtextbox
//Use for loop instead of foreach
for (int i = 0; i < rtextbox_firstname.Lines.Length; i++)
     csv.AppendLine($"{rtextbox_firstname.Lines[i]}, {rtextbox_surname.Lines[i]}, {rtextbox_country.Lines[i]}");


var path = "C:\Users\Shahi\Desktop\" + "run this " + "_" + date + ".csv";
File.AppendAllText(path, csv.ToString());