| 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: Write Text to a File |
03/30/2017 |
.net |
dotnet-standard |
article |
|
060cbe06-2adf-4337-9e7b-961a5c840208 |
29 |
mairaw |
mairaw |
wpickett |
How to: Write Text to a File
This topic shows different ways you can write text to a file for .NET Framework applications or [!INCLUDEwin8_appname_long] apps. The following classes and methods are typically used to write text to a file:
-
xref:System.IO.StreamWriter - it contains methods to write to a file synchronously (xref:System.IO.StreamWriter.Write%2A or 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 – to be used with .NET Framework applications. It 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 (xref:System.IO.File.AppendAllLines%2A, xref:System.IO.File.AppendAllText%2A or xref:System.IO.File.AppendText%2A).
-
FileIO - to be used with [!INCLUDEwin8_appname_long] apps. It contains asynchronous methods to write text to a file (WriteLinesAsync or WriteTextAsync) or to append text to a file (AppendLinesAsync or AppendTextAsync).
The samples have been kept simple in order to focus on the task being performed. For this reason, the samples perform minimal error checking and exception handling, if any. A real-world application generally provides more robust error checking and exception handling.
Example
The following example shows how to synchronously write text to a new file using the xref:System.IO.StreamWriter class, one line at a time. The new text file is saved to the user's My Documents folder. 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
The following example shows how to append text to an existing file using the xref:System.IO.StreamWriter class. It uses the same text file from the previous example.
[!code-csharpConceptual.BasicIO.TextFiles#AppendText] [!code-vbConceptual.BasicIO.TextFiles#AppendText]
Example
The following example shows how to asynchronously write text to a new file using the xref:System.IO.StreamWriter class. In order to invoke the xref:System.IO.StreamWriter.WriteAsync%2A method, the method call needs to be within an async method. The new text file is saved to the user's My Documents folder.
[!code-csharpConceptual.BasicIO.TextFiles#WriteAsync] [!code-vbConceptual.BasicIO.TextFiles#WriteAsync]
Example
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 will be overwritten.
[!code-csharpConceptual.BasicIO.TextFiles#WriteFile] [!code-vbConceptual.BasicIO.TextFiles#WriteFile]
Example
The following example shows how to asynchronously write user input to a text file in a [!INCLUDEwin8_appname_long] app. Because of security considerations, opening a file from a [!INCLUDEwin8_appname_long] app typically requires the use of a FileOpenPicker control. In this example, the FileOpenPicker is filtered to show text files.
<Page
x:Class="OpenFileWindowsStore.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:OpenFileWindowsStore"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Content="save text to a file" HorizontalAlignment="Left" Margin="103,417,0,0" VerticalAlignment="Top"
Width="329" Height="86" FontSize="35" Click="Button_Click"/>
<TextBox Name="UserInputTextBox" FontSize="18" HorizontalAlignment="Left" Margin="106,146,0,0"
TextWrapping="Wrap" Text="Write some text here, and select a file to write it to." VerticalAlignment="Top"
Height="201" Width="558" AcceptsReturn="True"/>
<TextBlock Name="StatusTextBox" HorizontalAlignment="Left" Margin="106,570,0,147" TextWrapping="Wrap" Text="Status:"
VerticalAlignment="Center" Height="51" Width="1074" FontSize="18" />
</Grid>
</Page> [!code-csharpOpenFileWindowsStore#Code] [!code-vbOpenFileWindowsStore#Code]
See Also
xref:System.IO.StreamWriter
xref:System.IO.File.CreateText%2A?displayProperty=nameWithType
How to: Enumerate Directories and Files
How to: Read and Write to a Newly Created Data File
How to: Open and Append to a Log File
How to: Read Text from a File
File and Stream I-O