Aracılığıyla paylaş


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 ref class PathIO 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 PathIO 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 PathIO
Public Class PathIO
Inheritance
Object Platform::Object IInspectable PathIO
Attributes

Windows requirements

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

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
}

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.

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<String>, UnicodeEncoding)

Appends lines of text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

AppendLinesAsync(String, IIterable<String>)

Appends lines of text to the file at the specified path or Uniform Resource Identifier (URI).

AppendTextAsync(String, String, UnicodeEncoding)

Appends text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

AppendTextAsync(String, String)

Appends text to the file at the specified path or Uniform Resource Identifier (URI).

ReadBufferAsync(String)

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

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.

ReadLinesAsync(String)

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

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.

ReadTextAsync(String)

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

WriteBufferAsync(String, IBuffer)

Writes data from a buffer to the file at the specified path or Uniform Resource Identifier (URI).

WriteBytesAsync(String, Byte[])

Writes a single byte of data to the file at the specified path or Uniform Resource Identifier (URI).

WriteLinesAsync(String, IIterable<String>, UnicodeEncoding)

Writes lines of text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

WriteLinesAsync(String, IIterable<String>)

Writes lines of text to the file at the specified path or Uniform Resource Identifier (URI).

WriteTextAsync(String, String, UnicodeEncoding)

Writes text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

WriteTextAsync(String, String)

Writes text to the file at the specified path or Uniform Resource Identifier (URI).

Applies to

See also