Export (0) Print
Expand All

How To: Rename a File

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

The Complete Sample

The code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.

To rename a save game file in title storage

  1. Call the Guide.BeginShowStorageDeviceSelector method to obtain a device index indicating which device the user prefers.
  2. Open a StorageContainer on the device, passing 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.Exists to confirm that the destination file does not exist.
  5. Call File.Move with the name of the file to rename, and the new name of the file.
  6. Dispose the StorageContainer to commit the changes to the device.
private static void DoRename(StorageDevice device)
{
    // Open a storage container.
    StorageContainer container =
        device.OpenContainer("StorageDemo");

    // Add the container path to our file name.
    string oldfilename = Path.Combine(container.Path, "demobinary.sav");
    string newfilename = Path.Combine(container.Path, "renamebinary.sav");

    if (!File.Exists(newfilename))
        File.Move(oldfilename, newfilename);

    // Dispose the container, to commit the change
    container.Dispose();
}

Community Additions

ADD
Show:
© 2014 Microsoft