Examples

You can use writeTextAsync(filePathOrURI, contents) to write text to a file. In the example, filePathOrURI is a local variable that contains a URI (like an app URI "ms-appdata://sampleFile.dat") or a file system path (like C:\examplepath\sampleFile.dat) for the file to read. Although the writeTextAsync methods don't have a return value, you can still perform additional tasks after the text is written to the file, as the example shows.

You can use readTextAsync(filePathOrURI) to read text from a file. After readTextAsync completes, the fileContent variable gets the contents of the file as a text string. You can then process the contents as appropriate.

Remarks

To read or write to a file using a path or Uniform Resource Identifier (URI), you must have permission to access the file's location. To learn more about what locations your app has permission to access, see File access permissions.

You can specify an app Uniform Resource Identifier (URI) to read or write files in app-specific locations like your app data folder ("ms-appdata://"). To learn more about app Uniform Resource Identifier (URI), see How to reference content.

Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) and returns a buffer.

public : static IAsyncOperation<IBuffer> ReadBufferAsync(PlatForm::String absolutePath)public static IAsyncOperation<IBuffer> ReadBufferAsync(String absolutePath)Public Static Function ReadBufferAsync(absolutePath As String) As IAsyncOperation( Of IBuffer )// You can use this method in JavaScript.

Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) and returns lines of text.

public : static IAsyncOperation<IVector<PlatForm::String>> ReadLinesAsync(PlatForm::String absolutePath)public static IAsyncOperation<IList<string>> ReadLinesAsync(String absolutePath)Public Static Function ReadLinesAsync(absolutePath As String) As IAsyncOperation( Of IListstring )// You can use this method in JavaScript.

Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) and returns text.

public : static IAsyncOperation<PlatForm::String> ReadTextAsync(PlatForm::String absolutePath)public static IAsyncOperation<string> ReadTextAsync(String absolutePath)Public Static Function ReadTextAsync(absolutePath As String) As IAsyncOperation( Of string )// You can use this method in JavaScript.

You can use then or done to declare a function to capture and process the text that was read from the file. After the readTextAsync method completes, the text will be passed to this function as a string object (contents in the example).

Remarks

This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. If an encoding cannot be detected, the encoding specified by the caller in the method overload is used.

Examples

This example shows you how to use writeTextAsync(absolutePath, contents) to write some arbitrary text to a file at the path in absoluteFilePath, like this:

Windows.Storage.FileIO.writeTextAsync(absoluteFilePath, "Swift as a shadow").then(function () {
// Add code to do something after the text is written to the file
});

Although the writeTextAsync methods don't have a return value, you can still use then or done to declare a function and perform additional tasks after the text is written to the file, as the sample shows.

Remarks

This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. If an encoding cannot be detected, the encoding specified by the caller in the method overload is used.