0

I thought it would just be as simple as

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());

It writes to the next file just fine;however, it writes them all on one line, so the output looks like

I'm the first item I'm the second item im the third item

instead of what i'm going for which is

I'm the first item
I'm the second item
I'm the third item

edited for formatting

3 Answers3

2

How about using File.WriteAllLines?

File.WriteAllLines("filepath", weeklyPlan.Items);
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • This seems to me to be the best solution, although the second parameter needs to be `weeklyPlan.Items.Cast()` for this to compile. – Enigmativity Apr 12 '16 at 04:16
0

Use AppendLine instead:

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

string pathing = @"C:\...";
System.IO.File.WriteAllText(pathing, buildStrings.ToString());
Rob
  • 26,989
  • 16
  • 82
  • 98
  • My word that was a headdesk simple fix, and i tried a ton of other things. My google skills are failing me apparently. Cheers – HoneyPunch Apr 11 '16 at 07:08
  • 1
    @HoneyPunch No worries :) Your code would work, depending on the platform. `\r\n` are typically the character(s) for a crlf on windows. Alternatively, you could use `Environment.NewLine` instead of `\n` for the same effect. – Rob Apr 11 '16 at 07:10
  • Environment.NewLine was actually the first thing i tried when "\n" failed me, but it didn't work. Hitting CtrlZ a few changes to get back to that point, i realize i had the entire " buildStrings.Append(Environment.NewLine);" commented out. I think its time for a break, lol. Thanks agian – HoneyPunch Apr 11 '16 at 07:15
0

You can use .AppendLine() method of stringBuilder Class. instead for .Append() Hence the loop may looks like the following:

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

And the difference between these two is that

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.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88