Dela via


FileIO Class

Definition

Provides helper methods for reading and writing files that are represented by objects of type IStorageFile.

public ref class FileIO abstract sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class FileIO final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public static class FileIO
Public Class FileIO
Inheritance
Object Platform::Object IInspectable FileIO
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

The File Access sample shows you how to use WriteTextAsync(file, contents) to write text to a file.

try
{
    if (file != null)
    {
        await FileIO.WriteTextAsync(file, "Swift as a shadow");
    }
}
catch (FileNotFoundException)
{
    // For example, handle file not found
}
try
{
    if (file)
    {
        co_await FileIO::WriteTextAsync(file, L"Swift as a shadow");
    }
}
catch (hresult_error const& ex)
{
    if (ex.code() == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
    {
        // 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.

try
{
    if (file != null)
    {
        string fileContent = await FileIO.ReadTextAsync(file);
    }
}
catch (FileNotFoundException)
{
    // For example, handle file not found
}
try
{
    if (file)
    {
        hstring fileContent = co_await FileIO::ReadTextAsync(file);
    }
}
catch (hresult_error const& ex)
{
    if (ex.code() == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
    {
        // 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 Create, write, and read a file.

Methods

AppendLinesAsync(IStorageFile, IIterable<String>, UnicodeEncoding)

Appends lines of text to the specified file using the specified character encoding.

AppendLinesAsync(IStorageFile, IIterable<String>)

Appends lines of text to the specified file.

AppendTextAsync(IStorageFile, String, UnicodeEncoding)

Appends text to the specified file using the specified character encoding.

AppendTextAsync(IStorageFile, String)

Appends text to the specified file.

ReadBufferAsync(IStorageFile)

Reads the contents of the specified file and returns a buffer.

ReadLinesAsync(IStorageFile, UnicodeEncoding)

Reads the contents of the specified file using the specified character encoding and returns lines of text.

ReadLinesAsync(IStorageFile)

Reads the contents of the specified file and returns lines of text.

ReadTextAsync(IStorageFile, UnicodeEncoding)

Reads the contents of the specified file using the specified character encoding and returns text.

ReadTextAsync(IStorageFile)

Reads the contents of the specified file and returns text.

WriteBufferAsync(IStorageFile, IBuffer)

Writes data from a buffer to the specified file.

WriteBytesAsync(IStorageFile, Byte[])

Writes an array of bytes of data to the specified file.

WriteLinesAsync(IStorageFile, IIterable<String>, UnicodeEncoding)

Writes lines of text to the specified file using the specified character encoding.

WriteLinesAsync(IStorageFile, IIterable<String>)

Writes lines of text to the specified file.

WriteTextAsync(IStorageFile, String, UnicodeEncoding)

Writes text to the specified file using the specified character encoding.

WriteTextAsync(IStorageFile, String)

Writes text to the specified file.

Applies to