Quickstart: Reading and writing files (XAML)
Read and write a file using a StorageFile object.
Prerequisites
-
Understand async programming for Windows Runtime apps using C++, C#, or Visual Basic
You can learn how to write asynchronous apps in Quickstart: Calling asynchronous APIs in C# or Visual Basic.
-
Know how to get the file that you want to read from, write to, or both
You can learn how to get a file by using a file picker in Quickstart: Accessing files with file pickers.
Creating a file
This section's code snippets illustrate how create a file (replacing it if it already exists) in the app's local folder. A StorageFile object named sampleFile is used in this topic's subsequent code snippets to reference the opened file.
Writing to a file
These steps show you how to write to a file if you have a writable file and a StorageFile that represents it.
Writing text to a file
-
Write text to your file by calling the WriteTextAsync methods of the FileIO class.
The File access sample shows you how to call WriteTextAsync(file, contents) to write some arbitrary text to its
Note Remember to put the async keyword on the method declaration of any method in which you use the await operator. For more information, see Quickstart: Calling asynchronous APIs in C# or Visual Basic.sampleFilelike this:
Writing bytes to a file by using a buffer
-
Get a buffer of the bytes that you want to write to your file.
For example, the File access sample calls ConvertStringToBinary to get a buffer of bytes based on an arbitrary string, like this:
-
Write the bytes from your buffer to your file by calling the WriteBufferAsync method of the FileIO class.
The File access sample shows you how to use WriteBufferAsync to write bytes from a buffer to its
sampleFile, like this:Note Remember to put the async keyword on the method declaration of any method in which you use the await operator. For more information, see Quickstart: Calling asynchronous APIs in C# or Visual Basic.
Writing text to a file by using a stream
-
Open the file by calling the StorageFile.OpenAsync method. It returns a stream of the file's content when the open operation completes.
The File access sample shows you how to open a stream for a file (
sampleFile) by calling the StorageFile.OpenAsync method, like this:Note Remember to put the async keyword on the method declaration of any method in which you use the await operator. For more information, see Quickstart: Calling asynchronous APIs in C# or Visual Basic. -
Get an output stream by calling the GetOutputStreamAt method from the
stream. Put this in a using statement to manage the output stream's lifetime.Use the following steps to add code within the using statement to write to the output stream.
-
Write text to the
outputStreamby creating a new DataWriter object and calling the DataWriter.WriteString method. -
Save the text to your file and close the stream by calling the
writer.StoreAsync andoutputStream.FlushAsync methods.The File access sample shows you how to save the text to your file and close the stream, like this:
Note Remember to put the async keyword on the method declaration of any method in which you use the await operator. For more information, see Quickstart: Calling asynchronous APIs in C# or Visual Basic.
-
You can download the File access sample to see these code examples in context inside methods.
Reading from a file
These steps show you how to read from a file if you have a readable file and a StorageFile that represents it. You can get a StorageFile that represents readable files by the StorageFolder.GetFileAsync method.
Reading text from a file
-
Read text from your file by calling the ReadTextAsync methods of the FileIO class.
The File access sample shows you how to read text from a file by calling ReadTextAsync(file) to read from its
Note Remember to put the async keyword on the method declaration of any method in which you use the await operator. For more information, see Quickstart: Calling asynchronous APIs in C# or Visual Basic.sampleFile, like this:
Reading bytes from a file by using a buffer
-
Read bytes from your buffer to your file by calling the ReadBufferAsync method of the FileIO class.
The File access sample shows you how to read bytes to a buffer from a file by calling ReadBufferAsync, like this:
Note Remember to put the async keyword on the method declaration of any method in which you use the await operator. For more information, see Quickstart: Calling asynchronous APIs in C# or Visual Basic.Then use a DataReader object to read the length of the
bufferand read the contents of the buffer.
Reading text from a file by using a stream
-
Open a stream for your file by calling the StorageFile.OpenAsync method. It returns a stream of the file's content when the open operation completes.
The File access sample shows you how to open a stream to a file (
sampleFile) by calling the StorageFile.OpenAsync method like this:Note Remember to put the async keyword on the method declaration of any method in which you use the await operator. For more information, see Quickstart: Calling asynchronous APIs in C# or Visual Basic. -
Get the size of the stream to use later.
-
Get an input stream by calling the GetInputStreamAt method of the
stream. Put this in a using statement to manage the input stream's lifetime. Specify 0 when you call GetInputStreamAt to set the position of theinputStreamat the beginning of the stream, as shown in the sample.Use the following steps to add code within the using statement to read the input stream.
-
Get a DataReader object by passing the
inputStreamto the constructor.The File access sample shows how to create a DataReader like this:
-
Read the text by calling the DataReader.LoadAsync and DataReader.ReadString methods.
The File access sample shows you how to read text and make sure the
streamisn't empty, like this:Note Remember to put the async keyword on the method declaration of any method in which you use the await operator. For more information, see Quickstart: Calling asynchronous APIs in C# or Visual Basic.
-
You can download the File access sample to see these code examples in context inside methods.
Summary and next steps
You should now understand how to read and write from a file if you have a StorageFile that represents the file.
To learn about working with image files, see Quickstart: Image and ImageBrush, Quickstart: Imaging, and the XAML image sample.
Related topics
- Accessing data and files
- Quickstart: Accessing files with file pickers
- Guidelines and checklist for file pickers
- File access and permissions
- File access sample
- XAML image sample
- Reference
- Windows.Storage.StorageFile class
- Windows.Storage.Streams.DataReader class
- Windows.Storage.Streams.DataWriter class