FileIO FileIO FileIO FileIO Class
Definition
Provides helper methods for reading and writing files that are represented by objects of type IStorageFile.
public : static class FileIOpublic static class FileIOPublic Static Class FileIO// 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
The File Access sample shows you how to use writeTextAsync(file, contents) to write text to a file.
if (file !== null) {
Windows.Storage.FileIO.writeTextAsync(file, "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
});
}
try
{
if (file != null)
{
await FileIO.WriteTextAsync(file, "Swift as a shadow");
}
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
// For example, handle file not found
}
In the example, file is a local variable that contains a storageFile that represents the file to write.
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.The File Access sample also shows you how to use readTextAsync(file) to read text from a file.
if (file !== null) {
Windows.Storage.FileIO.readTextAsync(file).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 FileIO.ReadTextAsync(file);
}
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
// For example, handle file not found
}
In the example, file is a local variable that contains a storageFile that represents the file to read.
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
This class is static and cannot be instantiated. Call the methods directly instead.
To learn more about what locations your app can access, see File access permissions.
To learn how to read and write to files, see Quickstart: Reading and writing a file.
Methods
AppendLinesAsync(IStorageFile, IIterable)
AppendLinesAsync(IStorageFile, IIterable)
AppendLinesAsync(IStorageFile, IIterable)
AppendLinesAsync(IStorageFile, IIterable)
Appends lines of text to the specified file.
public : static IAsyncAction AppendLinesAsync(IStorageFile file, IIterable<PlatForm::String> lines)public static IAsyncAction AppendLinesAsync(IStorageFile file, IEnumerable<String> lines)Public Static Function AppendLinesAsync(file As IStorageFile, lines As IEnumerable<String>) As IAsyncAction// You can use this method in JavaScript.
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(IStorageFile, IIterable, UnicodeEncoding)
AppendLinesAsync(IStorageFile, IIterable, UnicodeEncoding)
AppendLinesAsync(IStorageFile, IIterable, UnicodeEncoding)
AppendLinesAsync(IStorageFile, IIterable, UnicodeEncoding)
Appends lines of text to the specified file using the specified character encoding.
public : static IAsyncAction AppendLinesAsync(IStorageFile file, IIterable<PlatForm::String> lines, UnicodeEncoding encoding)public static IAsyncAction AppendLinesAsync(IStorageFile file, IEnumerable<String> lines, UnicodeEncoding encoding)Public Static Function AppendLinesAsync(file As IStorageFile, lines As IEnumerable<String>, encoding As UnicodeEncoding) As IAsyncAction// You can use this method in JavaScript.
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(IStorageFile, String) AppendTextAsync(IStorageFile, String) AppendTextAsync(IStorageFile, String) AppendTextAsync(IStorageFile, String)
Appends text to the specified file.
public : static IAsyncAction AppendTextAsync(IStorageFile file, PlatForm::String contents)public static IAsyncAction AppendTextAsync(IStorageFile file, String contents)Public Static Function AppendTextAsync(file As IStorageFile, contents As String) As IAsyncAction// You can use this method in JavaScript.
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(IStorageFile, String, UnicodeEncoding) AppendTextAsync(IStorageFile, String, UnicodeEncoding) AppendTextAsync(IStorageFile, String, UnicodeEncoding) AppendTextAsync(IStorageFile, String, UnicodeEncoding)
Appends text to the specified file using the specified character encoding.
public : static IAsyncAction AppendTextAsync(IStorageFile file, PlatForm::String contents, UnicodeEncoding encoding)public static IAsyncAction AppendTextAsync(IStorageFile file, String contents, UnicodeEncoding encoding)Public Static Function AppendTextAsync(file As IStorageFile, contents As String, encoding As UnicodeEncoding) As IAsyncAction// You can use this method in JavaScript.
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(IStorageFile) ReadBufferAsync(IStorageFile) ReadBufferAsync(IStorageFile) ReadBufferAsync(IStorageFile)
Reads the contents of the specified file and returns a buffer.
public : static IAsyncOperation<IBuffer> ReadBufferAsync(IStorageFile file)public static IAsyncOperation<IBuffer> ReadBufferAsync(IStorageFile file)Public Static Function ReadBufferAsync(file As IStorageFile) As IAsyncOperation( Of IBuffer )// You can use this method in JavaScript.
The file to read.
When this method completes, it returns an object (type IBuffer ) that represents the contents of the file.
Examples
The File Access sample shows you how to use ReadBufferAsync to read the contents of a file and return a buffer, like this:
try
{
if (file != null)
{
IBuffer buffer = await FileIO.ReadBufferAsync(file);
// Use a dataReader object to read from the buffer
using (DataReader dataReader = DataReader.FromBuffer(buffer))
{
string fileContent = dataReader.ReadString(buffer.Length);
// Perform additional tasks
}
}
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
// For example, handle file not found
}
if (file !== null) {
Windows.Storage.FileIO.readBufferAsync(sampleFile).then(function (buffer) {
// Perform additional tasks after file is read
// Use a dataReader object to read from the buffer
var dataReader = Windows.Storage.Streams.DataReader.fromBuffer(buffer);
var fileContent = dataReader.readString(buffer.length);
dataReader.close();
},
// Handle errors with an error function
function (error) {
// Handle errors encountered during write
});
}
In the example, file is a local variable that contains a storageFile that represents the file to read.
After readTextAsync completes, the buffer variable gets the contents of the file as an IBuffer object. You can then read from the buffer using a dataReader object and process the file contents as appropriate (as shown in the example.)
ReadLinesAsync(IStorageFile) ReadLinesAsync(IStorageFile) ReadLinesAsync(IStorageFile) ReadLinesAsync(IStorageFile)
Reads the contents of the specified file and returns lines of text.
public : static IAsyncOperation<IVector<PlatForm::String>> ReadLinesAsync(IStorageFile file)public static IAsyncOperation<IList<string>> ReadLinesAsync(IStorageFile file)Public Static Function ReadLinesAsync(file As IStorageFile) As IAsyncOperation( Of IListstring )// You can use this method in JavaScript.
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.
Remarks
This method uses the character encoding of the specified file. If you want to specify different encoding, call ReadLinesAsync(IStorageFile, UnicodeEncoding) instead.
- See Also
ReadLinesAsync(IStorageFile, UnicodeEncoding) ReadLinesAsync(IStorageFile, UnicodeEncoding) ReadLinesAsync(IStorageFile, UnicodeEncoding) ReadLinesAsync(IStorageFile, UnicodeEncoding)
Reads the contents of the specified file using the specified character encoding and returns lines of text.
public : static IAsyncOperation<IVector<PlatForm::String>> ReadLinesAsync(IStorageFile file, UnicodeEncoding encoding)public static IAsyncOperation<IList<string>> ReadLinesAsync(IStorageFile file, UnicodeEncoding encoding)Public Static Function ReadLinesAsync(file As IStorageFile, encoding As UnicodeEncoding) As IAsyncOperation( Of IListstring )// You can use this method in JavaScript.
The file to read.
The character encoding to use.
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(IStorageFile) ReadTextAsync(IStorageFile) ReadTextAsync(IStorageFile) ReadTextAsync(IStorageFile)
Reads the contents of the specified file and returns text.
public : static IAsyncOperation<PlatForm::String> ReadTextAsync(IStorageFile file)public static IAsyncOperation<string> ReadTextAsync(IStorageFile file)Public Static Function ReadTextAsync(file As IStorageFile) As IAsyncOperation( Of string )// You can use this method in JavaScript.
The file to read.
When this method completes successfully, it returns the contents of the file as a text string.
Remarks
Any object that implements the IStorageFile interface may be passed to this method or its overload through the file parameter.
This method uses the character encoding of the specified file. If you want to specify different encoding, call ReadTextAsync(IStorageFile, UnicodeEncoding) instead.
- See Also
ReadTextAsync(IStorageFile, UnicodeEncoding) ReadTextAsync(IStorageFile, UnicodeEncoding) ReadTextAsync(IStorageFile, UnicodeEncoding) ReadTextAsync(IStorageFile, UnicodeEncoding)
Reads the contents of the specified file using the specified character encoding and returns text.
public : static IAsyncOperation<PlatForm::String> ReadTextAsync(IStorageFile file, UnicodeEncoding encoding)public static IAsyncOperation<string> ReadTextAsync(IStorageFile file, UnicodeEncoding encoding)Public Static Function ReadTextAsync(file As IStorageFile, encoding As UnicodeEncoding) As IAsyncOperation( Of string )// You can use this method in JavaScript.
The file to read.
The character encoding to use.
When this method completes successfully, it returns the contents of the file as a text string.
- See Also
WriteBufferAsync(IStorageFile, IBuffer) WriteBufferAsync(IStorageFile, IBuffer) WriteBufferAsync(IStorageFile, IBuffer) WriteBufferAsync(IStorageFile, IBuffer)
Writes data from a buffer to the specified file.
public : static IAsyncAction WriteBufferAsync(IStorageFile file, IBuffer buffer)public static IAsyncAction WriteBufferAsync(IStorageFile file, IBuffer buffer)Public Static Function WriteBufferAsync(file As IStorageFile, buffer As IBuffer) As IAsyncAction// You can use this method in JavaScript.
The file that the buffer of data is written to.
No object or value is returned when this method completes.
Examples
The File Access sample shows you how to use WriteBufferAsync to write data from a buffer to a file.
try
{
if (file != null)
{
IBuffer buffer = GetBufferFromString("Swift as a shadow");
await FileIO.WriteBufferAsync(file, buffer);
// Perform additional tasks after file is written
}
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
// For example, handle file not found
}
if (file !== null) {
// Create buffer
var memoryStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
var dataWriter = new Windows.Storage.Streams.DataWriter(memoryStream);
dataWriter.writeString("Swift as a shadow");
var buffer = dataWriter.detachBuffer();
dataWriter.close();
// Write buffer to file
Windows.Storage.FileIO.writeBufferAsync(file, buffer).done(function () {
// Perform additional tasks after file is written
},
// Handle errors with an error function
function (error) {
// Handle errors encountered during write
});
}
In the example, file is a local variable that contains a storageFile that represents the file to write.
Although the writeBufferAsync 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.
WriteBytesAsync(IStorageFile, Byte[]) WriteBytesAsync(IStorageFile, Byte[]) WriteBytesAsync(IStorageFile, Byte[]) WriteBytesAsync(IStorageFile, Byte[])
Writes an array of bytes of data to the specified file.
public : static IAsyncAction WriteBytesAsync(IStorageFile file, Byte[] buffer)public static IAsyncAction WriteBytesAsync(IStorageFile file, Byte[] buffer)Public Static Function WriteBytesAsync(file As IStorageFile, buffer As Byte[]) As IAsyncAction// You can use this method in JavaScript.
The file that the byte is written to.
- buffer
- Byte[] Byte[] Byte[] Byte[]
The array of bytes to write.
No object or value is returned when this method completes.
WriteLinesAsync(IStorageFile, IIterable)
WriteLinesAsync(IStorageFile, IIterable)
WriteLinesAsync(IStorageFile, IIterable)
WriteLinesAsync(IStorageFile, IIterable)
Writes lines of text to the specified file.
public : static IAsyncAction WriteLinesAsync(IStorageFile file, IIterable<PlatForm::String> lines)public static IAsyncAction WriteLinesAsync(IStorageFile file, IEnumerable<String> lines)Public Static Function WriteLinesAsync(file As IStorageFile, lines As IEnumerable<String>) As IAsyncAction// You can use this method in JavaScript.
The file that the lines are written to.
- lines
- IIterable<PlatForm::String> IEnumerable<String> IEnumerable<String> IEnumerable<String>
The list of text strings to write 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 is used.
- See Also
WriteLinesAsync(IStorageFile, IIterable, UnicodeEncoding)
WriteLinesAsync(IStorageFile, IIterable, UnicodeEncoding)
WriteLinesAsync(IStorageFile, IIterable, UnicodeEncoding)
WriteLinesAsync(IStorageFile, IIterable, UnicodeEncoding)
Writes lines of text to the specified file using the specified character encoding.
public : static IAsyncAction WriteLinesAsync(IStorageFile file, IIterable<PlatForm::String> lines, UnicodeEncoding encoding)public static IAsyncAction WriteLinesAsync(IStorageFile file, IEnumerable<String> lines, UnicodeEncoding encoding)Public Static Function WriteLinesAsync(file As IStorageFile, lines As IEnumerable<String>, encoding As UnicodeEncoding) As IAsyncAction// You can use this method in JavaScript.
The file that the lines are written to.
- lines
- IIterable<PlatForm::String> IEnumerable<String> IEnumerable<String> IEnumerable<String>
The list of text strings to write as lines.
The character encoding of the file.
No object or value is returned when this method completes.
- See Also
WriteTextAsync(IStorageFile, String) WriteTextAsync(IStorageFile, String) WriteTextAsync(IStorageFile, String) WriteTextAsync(IStorageFile, String)
Writes text to the specified file.
public : static IAsyncAction WriteTextAsync(IStorageFile file, PlatForm::String contents)public static IAsyncAction WriteTextAsync(IStorageFile file, String contents)Public Static Function WriteTextAsync(file As IStorageFile, contents As String) As IAsyncAction// You can use this method in JavaScript.
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.
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 is used.
- See Also
WriteTextAsync(IStorageFile, String, UnicodeEncoding) WriteTextAsync(IStorageFile, String, UnicodeEncoding) WriteTextAsync(IStorageFile, String, UnicodeEncoding) WriteTextAsync(IStorageFile, String, UnicodeEncoding)
Writes text to the specified file using the specified character encoding.
public : static IAsyncAction WriteTextAsync(IStorageFile file, PlatForm::String contents, UnicodeEncoding encoding)public static IAsyncAction WriteTextAsync(IStorageFile file, String contents, UnicodeEncoding encoding)Public Static Function WriteTextAsync(file As IStorageFile, contents As String, encoding As UnicodeEncoding) As IAsyncAction// You can use this method in JavaScript.
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