Share via


Enumerating Save Game Files

Demonstrates how to use the StorageContainer class to get a list of save game files in the user storage area on a device specified by the gamer. This example assumes you obtained a StorageDevice, if not, see Getting a StorageDevice Asynchronously.

Note

The simplified example code demonstrates synchronous usage of BeginOpenContainer through the WaitOne method accessible through IAsyncResult.AsyncWaitHandle. The preferred technique to use this function asynchronously is similar to the technique demonstrated in the Getting a StorageDevice Asynchronously topic.

Complete Sample

The code in the topic shows you the technique for getting a list of save game files. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Download StorageDemo_Sample.zip

Enumerating Files

To enumerate save game files in user storage

  1. Call the StorageDevice.BeginShowSelector method to get a device index indicating which device the user prefers.

  2. Call BeginOpenContainer to open a StorageContainer on the device that is assigned the name of your title.

  3. Call GetFileNames.

    GetFileNames returns an array of strings, where each string is the name of a file in the container.

  4. Dispose the StorageContainer.

private static void DoEnumerate(StorageDevice device)
{
    IAsyncResult result =
        device.BeginOpenContainer("StorageDemo", null, null);

    // Wait for the WaitHandle to become signaled.
    result.AsyncWaitHandle.WaitOne();

    StorageContainer container = device.EndOpenContainer(result);

    // Close the wait handle.
    result.AsyncWaitHandle.Close();

    string[] FileList = container.GetFileNames();
    foreach (string filename in FileList)
    {
       Console.WriteLine(filename);
    }

    // Dispose the container.
    container.Dispose();
}

See Also

Concepts

What Is Storage?

Reference

StorageDevice
BeginShowSelector
StorageContainer