Windows UWP Storage API Reference

This document provides a comprehensive reference for the Windows Universal Platform (UWP) Storage API, enabling your applications to interact with the file system, access various storage locations, and manage files and folders.

Overview

The UWP Storage API provides a sandboxed and secure way for UWP applications to access storage. It abstracts away the complexities of underlying hardware and file systems, offering a consistent interface for common storage operations.

Key Concepts

Core Classes and Interfaces

Windows.Storage.StorageFolder

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

public sealed class StorageFolder : Windows.Storage.IStorageFolder, Windows.Storage.IStorageFolder2, Windows.Storage.IStorageItem, Windows.Storage.IStorageItemProperties, Windows.Foundation.IClosable

Common Members:

Windows.Storage.StorageFile

Represents a file in the file system. It allows you to read from and write to the file, and retrieve file properties.

public sealed class StorageFile : Windows.Storage.IStorageFile, Windows.Storage.IStorageFile2, Windows.Storage.IStorageItem, Windows.Storage.IStorageItemProperties, Windows.Foundation.IClosable

Common Members:

Windows.Storage.KnownFolders

Provides static properties to access predefined storage locations.

public static class KnownFolders

Common Properties:

Example: Reading a File from Local Storage

This example demonstrates how to read the content of a file named "settings.txt" from the application's local storage.


async void ReadLocalFile()
{
    try
    {
        // Get the local app data folder
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;

        // Get the file
        StorageFile sampleFile = await localFolder.GetFileAsync("settings.txt");

        // Read the file content
        string fileContent = await FileIO.ReadTextAsync(sampleFile);

        // Now 'fileContent' holds the text from the file
        System.Diagnostics.Debug.WriteLine("File content: " + fileContent);
    }
    catch (FileNotFoundException)
    {
        System.Diagnostics.Debug.WriteLine("File not found.");
        // Handle the case where the file doesn't exist
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("Error reading file: " + ex.Message);
    }
}
                

Example: Creating a Folder and File

This example shows how to create a new folder named "MyData" and a file named "log.txt" within it.


async void CreateFolderAndFile()
{
    try
    {
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;

        // Create a new folder
        StorageFolder dataFolder = await localFolder.CreateFolderAsync("MyData", CreationCollisionOption.OpenIfExists);

        // Create a new file inside the folder
        StorageFile logFile = await dataFolder.CreateFileAsync("log.txt", CreationCollisionOption.ReplaceExisting);

        // Write some content to the file
        await FileIO.WriteTextAsync(logFile, "Log entry: " + DateTime.Now.ToString());

        System.Diagnostics.Debug.WriteLine("Folder and file created successfully.");
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("Error creating folder/file: " + ex.Message);
    }
}
                

Related Topics