Deleting a Save Game File

Demonstrates how to use the StorageContainer class to delete a save game file 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.


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 deleting a save game file. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.


Deleting a File

To delete a saved game file 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 FileExists to confirm that the file is present.

  4. Call File.Delete with the name of the file to delete.

  5. Call the StorageContainer method to commit the changes to the device.

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

    // Wait for the WaitHandle to become signaled.

    StorageContainer container = device.EndOpenContainer(result);

    // Close the wait handle.

    // Add the container path to our file name.
    string filename = "demobinary.sav";

    if (container.FileExists(filename))

    // Dispose the container, to commit the change.

See Also


What Is Storage?