Search code examples
c#linqfileappendtext

AppendText method in C# (Windows Forms)


I am trying to learn C# and ADO.NET using this book: 'Accesing Data with Microsoft .NET Framework 4' by Glenn Johnson. In the third chapter, 'Introducing to LINQ', there is this code snippet:

foreach (var color in results)
{
    txtLog.AppendText(color + Environment.NewLine);
}

Since there are no details of how to create the txtLog file, I tried to do it this way:

StreamWriter txtLog=File.CreateText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"txtLog.txt"));
            foreach (var color in results)
                txtLog=File.AppendText(color + Environment.NewLine);

The problem is Environment.NewLine throws me an error: 'Illegal charactes in path'. After that, I learned that AppendText method takes as argument a path, which pretty much confuses me. How am I supposed to make the code from the book work? The snippet is used multiple times. Thanks.


Solution

  • Well, if you use Windows Forms you can create a new TextBox with the name txtLog. You can see here how to create one.

    Then your code will execute just fine without other modifications.