Quickstart: Determining availability of Microsoft OneDrive files (XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Determine if a Microsoft OneDrive file is available using the StorageFile.IsAvailable property.

Prerequisites

Many of the methods used to interact with folders and files are asynchronous. You can learn how to write asynchronous apps in Quickstart: Calling asynchronous APIs in C# or Visual Basic.

Using the StorageFile.IsAvailable property

In Windows 8.1, users are able to mark OneDrive files as either available-offline (default) or online-only. This capability enables users to move large files (such as pictures and videos) to their OneDrive, mark them as online-only, and save disk space (as a file containing only metadata about the remote file is kept locally).

A new property, StorageFile.IsAvailable, is used to determine if a file is currently available. The following table shows the value of the StorageFile.IsAvailable property in various scenarios.

Type of file Online Metered network Offline
Local file True True True
OneDrive file marked as available-offline True True True
OneDrive file marked as online-only True Based on user settings False
Network file True Based on user settings False

 

The following steps illustrate how to determine if a file is currently available.

  1. Define the appropriate library access capabilities. Depending on the library that is being enumerated, you'll need to define the capability to access that library. To learn more about this and defining other file-level capabilities for Microsoft Visual Studio projects, see File access and permissions for Windows Store apps
  2. Include the Windows.Storage namespace. This namespace includes the types for managing files, folders, and application settings. It also includes the needed StorageFile type.
  3. Acquire a StorageFile object for the desired file(s). If you are enumerating a library, this step is usually accomplished by calling the StorageFolder.CreateFileQuery method and then calling the resulting StorageFileQueryResult object's GetFilesAsync method. The GetFilesAsync method returns an IReadOnlyList collection of StorageFile objects.
  4. Once you have the access to a StorageFile object representing the desired file(s), the value of the StorageFile.IsAvailable property reflects whether or not the file is available.

The following generic method illustrates how to enumerate any library and return the collection of StorageFile objects for that library. The calling method then iterates over the returned collection referencing the StorageFile.IsAvailable property for each file.

/// <summary>
/// Generic function that retrieves all files from the specified folder.
/// </summary>
/// <param name="folder">The folder to be searched.</param>
/// <returns>An IReadOnlyList collection containing the file objects.</returns>
async Task<System.Collections.Generic.IReadOnlyList<StorageFile>> GetLibraryFilesAsync(StorageFolder folder)
{
    var query = folder.CreateFileQuery();
    return await query.GetFilesAsync();
}

...

private async void CheckAvailabilityOfFilesInPicturesLibrary()
{
    // Determine availability of all files within Pictures library.
    var files = await GetLibraryFilesAsync(KnownFolders.PicturesLibrary);
    for (int i = 0; i < files.Count; i++)
    {
        StorageFile file = files[i];

        StringBuilder fileInfo = new StringBuilder();
        fileInfo.AppendFormat("{0} (on {1}) is {2}", 
                    file.Name, 
                    file.Provider.DisplayName, 
                    file.IsAvailable ? "available" : "not available");
    }
}

Summary

In this quickstart, you learned about smart files and how to programmatically determine if a file is available.