Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How To: Open a File

This example demonstrates how to use the StorageContainer class to open 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 open 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 file to be opened.
  4. Call File.Open with the file name of the file to open. File.Open returns a FileStream object that you can use to add data to the file.
  5. Close the file after you have read it.
  6. Dispose the StorageContainer after closing the file.
private static void DoOpen(StorageDevice device)
    // Open a storage container.
    StorageContainer container =

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

    FileStream file = File.Open(filename, FileMode.Open);

    // Dispose the container.

Community Additions

© 2015 Microsoft