Windows Dev Center

UWP Storage APIs

The Universal Windows Platform (UWP) provides a robust set of APIs for accessing and managing files and folders on a device. These APIs are designed to work across all Windows 10 and Windows 11 devices, ensuring a consistent experience.

Key Concepts

UWP storage APIs focus on providing secure and authorized access to user data. Apps are typically granted access to specific locations, such as their app-specific folders, user's documents, pictures, or removable storage.

File Access

Accessing individual files is fundamental. The Windows.Storage.StorageFile class represents a file and provides methods to open, read, write, and manage its properties.

StorageFile.OpenAsync()

Opens a file for reading or writing. Returns a RandomAccessStream.


async Task<IRandomAccessStream> OpenFileAsync(StorageFile file, FileAccessMode accessMode)
{
    return await file.OpenAsync(accessMode);
}
                    
Parameters
Name Type Description
file StorageFile The file to open.
accessMode FileAccessMode Specifies whether to open for reading (FileAccessMode.Read) or reading and writing (FileAccessMode.ReadWrite).
Returns
Task<IRandomAccessStream>: A task that represents the asynchronous operation. The result of the task is an IRandomAccessStream that can be used to read from or write to the file.

Folder Access

Navigating and managing directories is handled by the Windows.Storage.StorageFolder class. You can get specific folders (like Pictures, Documents) or access subfolders and files within a folder.

StorageFolder.GetFilesAsync()

Retrieves a list of files in the current folder.


async Task<IReadOnlyList<StorageFile>> GetFilesInFolderAsync(StorageFolder folder)
{
    return await folder.GetFilesAsync();
}
                    
Parameters
Name Type Description
folder StorageFolder The folder to query.
Returns
Task<IReadOnlyList<StorageFile>>: A task that represents the asynchronous operation. The result is a read-only list of StorageFile objects.
StorageFolder.CreateFileAsync()

Creates a new file in the current folder.


async Task<StorageFile> CreateNewFileAsync(StorageFolder folder, string desiredName)
{
    return await folder.CreateFileAsync(desiredName);
}
                    
Parameters
Name Type Description
folder StorageFolder The folder where the file will be created.
desiredName string The name of the new file.
Returns
Task<StorageFile>: A task that represents the asynchronous operation. The result is the newly created StorageFile.

Data Storage

UWP provides specific locations for app data, ensuring data is isolated and managed correctly. These include Local App Data, Roaming App Data, and Temporary App Data.

ApplicationData.Current.LocalFolder

Gets the local app data folder. This storage is private to the app and not synced.


StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                    
ApplicationData.Current.RoamingSettings

Provides access to settings that roam with the user's account across devices.


ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
localSettings.Values["LastUsername"] = "User123";
                    

Key Classes and Interfaces

  • Windows.Storage.StorageFile: Represents a file.
  • Windows.Storage.StorageFolder: Represents a directory.
  • Windows.Storage.IStorageItem: Base interface for files and folders.
  • Windows.Storage.FileAccessMode: Enum for specifying file access permissions.
  • Windows.Storage.ApplicationData: Provides access to app-specific data storage.
  • Windows.Storage.Streams.IRandomAccessStream: Interface for reading and writing data with random access.

For more detailed information and advanced scenarios, please refer to the official UWP File Access documentation.