PathIO PathIO PathIO PathIO Class
Definition
Provides helper methods for reading and writing a file using the absolute path or Uniform Resource Identifier (URI) of the file.
public : static class PathIOpublic static class PathIOPublic Static Class PathIO// You can use this class in JavaScript.
- Attributes
| Device family |
Windows 10 (introduced v10.0.10240.0)
|
| API contract |
Windows.Foundation.UniversalApiContract (introduced v1)
|
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.
try
{
if (file != null)
{
await PathIO.WriteTextAsync(filePathOrURI, "Swift as a shadow");
}
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
// For example, handle file not found
}
if (file !== null) {
Windows.Storage.PathIO.writeTextAsync(filePathOrURI, "Swift as a shadow").done(function () {
// Perform additional tasks after file is written
},
// Handle errors with an error function
function (error) {
// Handle errors encountered during write
});
}
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.
if (file !== null) {
Windows.Storage.PathIO.readTextAsync(filePathOrURI).done(function (fileContent) {
// Process content read from the file
},
// Handle errors with an error function
function (error) {
// Handle errors encountered during read
});
}
try
{
if (file != null)
{
string fileContent = await PathIO.ReadTextAsync(filePathOrURI);
}
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
// For example, handle file not found
}
For more, related examples, see File Access sample and FileIO.
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.
Methods
AppendLinesAsync(String, IIterable)
AppendLinesAsync(String, IIterable)
AppendLinesAsync(String, IIterable)
AppendLinesAsync(String, IIterable)
Appends lines of text to the file at the specified path or Uniform Resource Identifier (URI).
public : static IAsyncAction AppendLinesAsync(PlatForm::String absolutePath, IIterable<PlatForm::String> lines)public static IAsyncAction AppendLinesAsync(String absolutePath, IEnumerable<String> lines)Public Static Function AppendLinesAsync(absolutePath As String, lines As IEnumerable<String>) As IAsyncAction// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path or Uniform Resource Identifier (URI) of the file that the lines are appended to.
- lines
- IIterable<PlatForm::String> IEnumerable<String> IEnumerable<String> IEnumerable<String>
The list of text strings to append as lines.
No object or value is returned when this method completes.
- See Also
AppendLinesAsync(String, IIterable, UnicodeEncoding)
AppendLinesAsync(String, IIterable, UnicodeEncoding)
AppendLinesAsync(String, IIterable, UnicodeEncoding)
AppendLinesAsync(String, IIterable, UnicodeEncoding)
Appends lines of text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.
public : static IAsyncAction AppendLinesAsync(PlatForm::String absolutePath, IIterable<PlatForm::String> lines, UnicodeEncoding encoding)public static IAsyncAction AppendLinesAsync(String absolutePath, IEnumerable<String> lines, UnicodeEncoding encoding)Public Static Function AppendLinesAsync(absolutePath As String, lines As IEnumerable<String>, encoding As UnicodeEncoding) As IAsyncAction// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path of the file that the lines are appended to.
- lines
- IIterable<PlatForm::String> IEnumerable<String> IEnumerable<String> IEnumerable<String>
The list of text strings to append as lines.
The character encoding of the file.
No object or value is returned when this method completes.
- See Also
AppendTextAsync(String, String) AppendTextAsync(String, String) AppendTextAsync(String, String) AppendTextAsync(String, String)
Appends text to the file at the specified path or Uniform Resource Identifier (URI).
public : static IAsyncAction AppendTextAsync(PlatForm::String absolutePath, PlatForm::String contents)public static IAsyncAction AppendTextAsync(String absolutePath, String contents)Public Static Function AppendTextAsync(absolutePath As String, contents As String) As IAsyncAction// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path of the file that the text is appended to.
- contents
- PlatForm::String String String String
The text to append.
No object or value is returned when this method completes.
- See Also
AppendTextAsync(String, String, UnicodeEncoding) AppendTextAsync(String, String, UnicodeEncoding) AppendTextAsync(String, String, UnicodeEncoding) AppendTextAsync(String, String, UnicodeEncoding)
Appends text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.
public : static IAsyncAction AppendTextAsync(PlatForm::String absolutePath, PlatForm::String contents, UnicodeEncoding encoding)public static IAsyncAction AppendTextAsync(String absolutePath, String contents, UnicodeEncoding encoding)Public Static Function AppendTextAsync(absolutePath As String, contents As String, encoding As UnicodeEncoding) As IAsyncAction// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path of the file that the text is appended to.
- contents
- PlatForm::String String String String
The text to append.
The character encoding of the file.
No object or value is returned when this method completes.
- See Also
ReadBufferAsync(String) ReadBufferAsync(String) ReadBufferAsync(String) ReadBufferAsync(String)
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.
- absolutePath
- PlatForm::String String String String
The path of the file to read.
When this method completes, it returns an object (type IBuffer ) that represents the contents of the file.
ReadLinesAsync(String) ReadLinesAsync(String) ReadLinesAsync(String) ReadLinesAsync(String)
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.
- absolutePath
- PlatForm::String String String String
The path of the file to read.
When this method completes successfully, it returns the contents of the file as a list (type IVector ) of lines of text. Each line of text in the list is represented by a String object.
- See Also
ReadLinesAsync(String, UnicodeEncoding) ReadLinesAsync(String, UnicodeEncoding) ReadLinesAsync(String, UnicodeEncoding) ReadLinesAsync(String, UnicodeEncoding)
Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding and returns lines of text.
public : static IAsyncOperation<IVector<PlatForm::String>> ReadLinesAsync(PlatForm::String absolutePath, UnicodeEncoding encoding)public static IAsyncOperation<IList<string>> ReadLinesAsync(String absolutePath, UnicodeEncoding encoding)Public Static Function ReadLinesAsync(absolutePath As String, encoding As UnicodeEncoding) As IAsyncOperation( Of IListstring )// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path of the file to read.
The character encoding of the file.
When this method completes successfully, it returns the contents of the file as a list (type IVector ) of lines of text. Each line of text in the list is represented by a String object.
- See Also
ReadTextAsync(String) ReadTextAsync(String) ReadTextAsync(String) ReadTextAsync(String)
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.
- absolutePath
- PlatForm::String String String String
The path of the file to read.
When this method completes successfully, it returns the contents of the file as a text string.
Examples
This example shows you how to use readTextAsync(absolutePath) to read text from the file at the path in absoluteFilePath like this:
Windows.Storage.PathIO.readTextAsync(absoluteFilePath).then(function (contents) {
// Add code to process the text read from the file
});
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).
- See Also
ReadTextAsync(String, UnicodeEncoding) ReadTextAsync(String, UnicodeEncoding) ReadTextAsync(String, UnicodeEncoding) ReadTextAsync(String, UnicodeEncoding)
Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding and returns text.
public : static IAsyncOperation<PlatForm::String> ReadTextAsync(PlatForm::String absolutePath, UnicodeEncoding encoding)public static IAsyncOperation<string> ReadTextAsync(String absolutePath, UnicodeEncoding encoding)Public Static Function ReadTextAsync(absolutePath As String, encoding As UnicodeEncoding) As IAsyncOperation( Of string )// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path of the file to read.
The character encoding of the file.
When this method completes successfully, it returns the contents of the file as a text string.
- See Also
WriteBufferAsync(String, IBuffer) WriteBufferAsync(String, IBuffer) WriteBufferAsync(String, IBuffer) WriteBufferAsync(String, IBuffer)
Writes data from a buffer to the file at the specified path or Uniform Resource Identifier (URI).
public : static IAsyncAction WriteBufferAsync(PlatForm::String absolutePath, IBuffer buffer)public static IAsyncAction WriteBufferAsync(String absolutePath, IBuffer buffer)Public Static Function WriteBufferAsync(absolutePath As String, buffer As IBuffer) As IAsyncAction// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path of the file that the data is written to.
No object or value is returned when this method completes.
WriteBytesAsync(String, Byte[]) WriteBytesAsync(String, Byte[]) WriteBytesAsync(String, Byte[]) WriteBytesAsync(String, Byte[])
Writes a single byte of data to the file at the specified path or Uniform Resource Identifier (URI).
public : static IAsyncAction WriteBytesAsync(PlatForm::String absolutePath, Byte[] buffer)public static IAsyncAction WriteBytesAsync(String absolutePath, Byte[] buffer)Public Static Function WriteBytesAsync(absolutePath As String, buffer As Byte[]) As IAsyncAction// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path of the file that the byte is written to.
- buffer
- Byte[] Byte[] Byte[] Byte[]
An array of bytes to write.
No object or value is returned when this method completes.
WriteLinesAsync(String, IIterable)
WriteLinesAsync(String, IIterable)
WriteLinesAsync(String, IIterable)
WriteLinesAsync(String, IIterable)
Writes lines of text to the file at the specified path or Uniform Resource Identifier (URI).
public : static IAsyncAction WriteLinesAsync(PlatForm::String absolutePath, IIterable<PlatForm::String> lines)public static IAsyncAction WriteLinesAsync(String absolutePath, IEnumerable<String> lines)Public Static Function WriteLinesAsync(absolutePath As String, lines As IEnumerable<String>) As IAsyncAction// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path of the file that the lines are written to.
- lines
- IIterable<PlatForm::String> IEnumerable<String> IEnumerable<String> IEnumerable<String>
The list of text strings to append as lines.
No object or value is returned when this method completes.
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.
- See Also
WriteLinesAsync(String, IIterable, UnicodeEncoding)
WriteLinesAsync(String, IIterable, UnicodeEncoding)
WriteLinesAsync(String, IIterable, UnicodeEncoding)
WriteLinesAsync(String, IIterable, UnicodeEncoding)
Writes lines of text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.
public : static IAsyncAction WriteLinesAsync(PlatForm::String absolutePath, IIterable<PlatForm::String> lines, UnicodeEncoding encoding)public static IAsyncAction WriteLinesAsync(String absolutePath, IEnumerable<String> lines, UnicodeEncoding encoding)Public Static Function WriteLinesAsync(absolutePath As String, lines As IEnumerable<String>, encoding As UnicodeEncoding) As IAsyncAction// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path of the file that the lines are appended to.
- lines
- IIterable<PlatForm::String> IEnumerable<String> IEnumerable<String> IEnumerable<String>
The list of text strings to append as lines.
The character encoding of the file.
No object or value is returned when this method completes.
- See Also
WriteTextAsync(String, String) WriteTextAsync(String, String) WriteTextAsync(String, String) WriteTextAsync(String, String)
Writes text to the file at the specified path or Uniform Resource Identifier (URI).
public : static IAsyncAction WriteTextAsync(PlatForm::String absolutePath, PlatForm::String contents)public static IAsyncAction WriteTextAsync(String absolutePath, String contents)Public Static Function WriteTextAsync(absolutePath As String, contents As String) As IAsyncAction// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path of the file that the text is written to.
- contents
- PlatForm::String String String String
The text to write.
No object or value is returned when this method completes.
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.
- See Also
WriteTextAsync(String, String, UnicodeEncoding) WriteTextAsync(String, String, UnicodeEncoding) WriteTextAsync(String, String, UnicodeEncoding) WriteTextAsync(String, String, UnicodeEncoding)
Writes text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.
public : static IAsyncAction WriteTextAsync(PlatForm::String absolutePath, PlatForm::String contents, UnicodeEncoding encoding)public static IAsyncAction WriteTextAsync(String absolutePath, String contents, UnicodeEncoding encoding)Public Static Function WriteTextAsync(absolutePath As String, contents As String, encoding As UnicodeEncoding) As IAsyncAction// You can use this method in JavaScript.
- absolutePath
- PlatForm::String String String String
The path of the file that the text is written to.
- contents
- PlatForm::String String String String
The text to write.
The character encoding of the file.
No object or value is returned when this method completes.
- See Also