Skip to content

Latest commit

 

History

History
68 lines (48 loc) · 4.79 KB

how-to-write-text-to-a-file.md

File metadata and controls

68 lines (48 loc) · 4.79 KB
title description ms.date ms.custom dev_langs helpviewer_keywords ms.assetid
How to: Write text to a file
Learn ways to write or append text to a file for a .NET app. Use methods from the StreamWriter or File classes to write text synchronously or asynchronously.
03/15/2024
devdivchpfy22
csharp
vb
writing text to files
I/O [.NET], writing text to files
streams, writing text to files
data streams, writing text to files
060cbe06-2adf-4337-9e7b-961a5c840208

How to: Write text to a file

This article shows different ways to write text to a file for a .NET app.

The following classes and methods are typically used to write text to a file:

  • xref:System.IO.StreamWriter contains methods to write to a file synchronously (xref:System.IO.StreamWriter.Write%2A and xref:System.IO.TextWriter.WriteLine%2A) or asynchronously (xref:System.IO.StreamWriter.WriteAsync%2A and xref:System.IO.StreamWriter.WriteLineAsync%2A).

  • xref:System.IO.File provides static methods to write text to a file such as xref:System.IO.File.WriteAllLines%2A and xref:System.IO.File.WriteAllText%2A, or to append text to a file such as xref:System.IO.File.AppendAllLines%2A, xref:System.IO.File.AppendAllText%2A, and xref:System.IO.File.AppendText%2A.

  • xref:System.IO.Path is for strings that have file or directory path information. It contains the xref:System.IO.Path.Combine%2A method and in .NET Core 2.1 and later, the xref:System.IO.Path.Join%2A and xref:System.IO.Path.TryJoin%2A methods. These methods let you concatenate strings for building a file or directory path.

Note

The following examples show only the minimum amount of code needed. A real-world app usually provides more robust error checking and exception handling.

Example: Synchronously write text with StreamWriter

The following example shows how to use the xref:System.IO.StreamWriter class to synchronously write text to a new file one line at a time. Because the xref:System.IO.StreamWriter object is declared and instantiated in a using statement, the xref:System.IO.StreamWriter.Dispose%2A method is invoked, which automatically flushes and closes the stream.

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

Example: Synchronously append text with StreamWriter

The following example shows how to use the xref:System.IO.StreamWriter class to synchronously append text to the text file created in the first example:

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

Example: Asynchronously write text with StreamWriter

The following example shows how to asynchronously write text to a new file using the xref:System.IO.StreamWriter class. To invoke the xref:System.IO.StreamWriter.WriteAsync%2A method, the method call must be within an async method.

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

Example: Write and append text with the File class

The following example shows how to write text to a new file and append new lines of text to the same file using the xref:System.IO.File class. The xref:System.IO.File.WriteAllText%2A and xref:System.IO.File.AppendAllLines%2A methods open and close the file automatically. If the path you provide to the xref:System.IO.File.WriteAllText%2A method already exists, the file is overwritten.

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

See also