Permalink
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (41 sloc) 2.64 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Open and Append to a Log File
03/30/2017
.net
dotnet-standard
article
log files, opening
streams, opening and appending to log file
log files, appending to
I/O [.NET Framework], log files
74423362-1721-49cb-aa0a-e04005f72a06
15
mairaw
mairaw
wpickett

How to: Open and Append to a Log File

xref:System.IO.StreamWriter and xref:System.IO.StreamReader write characters to and read characters from streams. The following code example opens the log.txt file for input, or creates the file if it does not already exist, and appends information to the end of the file. The contents of the file are then written to standard output for display. As an alternative to this example, the information could be stored as a single string or string array, and the xref:System.IO.File.WriteAllText%2A or xref:System.IO.File.WriteAllLines%2A method could be used to achieve the same functionality.

[!NOTE] Visual Basic users may choose to use the methods and properties provided by the xref:Microsoft.VisualBasic.Logging.Log class or xref:Microsoft.VisualBasic.FileIO.FileSystem class for creating or writing to log files.

Example

[!code-csharpConceptual.BasicIO.TextFiles#2] [!code-vbConceptual.BasicIO.TextFiles#2]

See Also

xref:System.IO.StreamWriter
xref:System.IO.StreamReader
xref:System.IO.File.AppendText%2A?displayProperty=nameWithType
xref:System.IO.File.OpenText%2A?displayProperty=nameWithType
xref:System.IO.StreamReader.ReadLine%2A?displayProperty=nameWithType
How to: Enumerate Directories and Files
How to: Read and Write to a Newly Created Data File
How to: Read Text from a File
How to: Write Text to a File
How to: Read Characters from a String
How to: Write Characters to a String
File and Stream I-O