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
- StorageFolder: Represents a folder in the file system.
- StorageFile: Represents a file in the file system.
- KnownFolders: Provides access to well-known locations like Documents, Pictures, Videos, and the app's local/roaming data folders.
- File Access Permissions: Applications must declare the necessary capabilities in their manifest to access certain storage locations.
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.
Common Members:
- GetFilesAsync(): Asynchronously retrieves a list of files in the folder.
- GetFoldersAsync(): Asynchronously retrieves a list of subfolders in the folder.
- CreateFileAsync(string desiredName): Asynchronously creates a new file with the specified name.
- CreateFolderAsync(string desiredName): Asynchronously creates a new subfolder with the specified name.
- DeleteAsync(): Asynchronously deletes the folder and its contents.
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.
Common Members:
- OpenAsync(FileAccessMode desiredAccess): Asynchronously opens the file for reading or writing.
- OpenSequentialReadAsync(): Asynchronously opens the file for sequential reading.
- CopyAsync(IStorageFolder destinationFolder): Asynchronously copies the file to another folder.
- MoveAsync(IStorageFolder destinationFolder): Asynchronously moves the file to another folder.
- DeleteAsync(): Asynchronously deletes the file.
Windows.Storage.KnownFolders
Provides static properties to access predefined storage locations.
Common Properties:
- Local: Gets the app's local app data folder.
- Roaming: Gets the app's roaming app data folder.
- Temporary: Gets the app's temporary app data folder.
- DocumentsLibrary: Gets the Documents library folder.
- PicturesLibrary: Gets the Pictures library folder.
- VideosLibrary: Gets the Videos library folder.
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);
}
}