C# 在单独的行上编写 ComboBox 项

C# Write ComboBox items on separate lines

我以为它会像

一样简单
StringBuilder buildStrings = new StringBuilder();
foreach (string day in weeklyPlan.Items)
{
    buildStrings.Append(day);
    buildStrings.Append("\n");

}
string pathing = @"C:\...";
System.IO.File.WriteAllText(pathing, buildStrings.ToString());

它可以很好地写入下一个文件;但是,它会将它们全部写入一行,所以输出看起来像

我是第一个我是第二个我是第三个

而不是我想要的是

我是第一个项目
我是第二条
我是第三条

已编辑格式

改用AppendLine

StringBuilder buildStrings = new StringBuilder();
foreach (string day in weeklyPlan.Items)
    buildStrings.AppendLine(day);

string pathing = @"C:\...";
System.IO.File.WriteAllText(pathing, buildStrings.ToString());

您可以使用 .AppendLine() method of stringBuilder Class. 代替 .Append() 因此循环可能如下所示:

foreach (string day in weeklyPlan.Items)
{
    buildStrings.AppendLine(day);
}

而这两者的区别在于

AppendLine() will append its argument, followed by Environment.Newline. If you don't call AppendLine(), you will need to include the newline yourself along with the appended string as you did. but use Environment.NewLine instead for \n as Rob suggested.

使用File.WriteAllLines怎么样?

File.WriteAllLines("filepath", weeklyPlan.Items);