Windows UWP API Reference

Storage API

The Windows Universal Platform (UWP) Storage APIs provide a comprehensive set of tools for managing files and folders within your UWP applications. These APIs allow you to access local storage, cloud storage, and external storage devices in a secure and efficient manner.

This section covers key classes, interfaces, and methods for interacting with the file system in UWP applications.

Core Concepts

Accessing Files and Folders

UWP applications operate within a sandboxed environment, meaning they have specific access permissions to storage locations. The Storage APIs abstract these permissions, providing a consistent way to access user-generated content and app-specific data.

Key Classes

Key APIs

StorageFile

Represents a file in the file system. It provides methods for reading, writing, and managing file properties.


class StorageFile
{
    // Properties
    string Name { get; }
    string FileType { get; }
    string ContentType { get; }
    DateTimeOffset DateCreated { get; }

    // Methods
    IAsyncOperation<IBuffer> OpenReadAsync();
    IAsyncOperation<IOutputStream> OpenWriteAsync();
    IAsyncOperation<IRandomAccessStream> OpenAsync(FileAccessMode accessMode);
    IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder);
    IAsyncOperation<StorageFile> MoveAsync(IStorageFolder destinationFolder);
    IAsyncOperation<StorageFile> RenameAsync(string desiredName);
    IAsyncOperation<StorageLibraryChangeTrackerOptions> GetLibraryChangeTracker();
}
            

StorageFolder

Represents a folder in the file system. It allows you to enumerate files and subfolders, create new files and folders, and delete items.


class StorageFolder
{
    // Properties
    string Name { get; }
    string Path { get; }

    // Methods
    IAsyncOperation<IReadOnlyList<StorageFile>> GetFilesAsync();
    IAsyncOperation<IReadOnlyList<StorageFolder>> GetFoldersAsync();
    IAsyncOperation<StorageFile> GetFileAsync(string name);
    IAsyncOperation<StorageFolder> GetFolderAsync(string name);
    IAsyncOperation<StorageFile> CreateFileAsync(string desiredName);
    IAsyncOperation<StorageFolder> CreateFolderAsync(string desiredName);
    IAsyncOperation<StorageLibraryChangeTrackerOptions> GetLibraryChangeTracker();
}
            

StorageStats

Provides information about storage usage, such as available free space and the size of files and folders.


class StorageStats
{
    ulong Size { get; }
    ulong ReservedFreeSpace { get; }
    ulong UsedSpace { get; }
}
            

IRandomAccessStream

An interface that represents a stream that can be accessed randomly. Used for reading and writing file data.


interface IRandomAccessStream : IInputStream, IOutputStream
{
    ulong Position { get; }
    ulong Size { get; set; }
    IRandomAccessStream CloneStream();
    void Seek(ulong position);
}
            

IAsyncOperation

Represents an asynchronous operation that returns a result. UWP APIs heavily rely on asynchronous operations to avoid blocking the UI thread.


interface IAsyncOperation<TResult> : IAsyncInfo
{
    TResult GetResults();
}
            

Common Scenarios

Best Practices