How To: Copy a File

Demonstrates how to use the StorageContainer class to copy a save game file in the title storage area on a device specified by the gamer. This example assumes you already obtained a StorageDevice. To obtain a StorageDevice, see How To: Get a StorageDevice Asynchronously.

The Complete Sample

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

Copying a File

To copy a save game file in title storage

  1. Call the Guide.BeginShowStorageDeviceSelector method to get a device index indicating which device the user prefers.

  2. Open a StorageContainer on the device, and then pass the name of your title.

  3. Call Path.Combine to merge the container path with the name of the source and destination files.

  4. Call File.Copy with the file name of the file to copy, and the name of the new copy to be created.

    You may also specify whether to overwrite existing files.

  5. Dispose the StorageContainer to commit the changes to the device.

private static void DoCopy(StorageDevice device)
    // Open a storage container.
    StorageContainer container =

    // Add the container path to our file name.
    string filename = Path.Combine(container.Path, "demobinary.sav");
    string copyfilename = Path.Combine(container.Path, "copybinary.sav");

    File.Copy(filename, copyfilename, true);

    // Dispose the container, to commit the change.

Community Additions