What Is Storage?
When programming with the XNA Framework, you read files from two general locations:
- Title storage is where the game executable and support files (shaders, meshes, textures, and so on) are located.
- User storage is the space a player provides for game data (such as saves) that is either locked to a particular profile or available to all players.
Title storage refers to the read-only storage in the default game storage location, including the game executable and all other files packaged with the game. Data in title storage may be accessed through one of two ways:
Typically, game assets (such as textures, shaders, media, and custom data) are accessed through the Content Pipeline.
The assets managed through these facilities are not accessed directly as files, but instead are loaded and unloaded through ContentManager by reference to the names they were given at build-time.
Your XNA Game Studio game should use the Content Pipeline, as it offers the most flexible and efficient facilities to manage these assets, and assures speedy operation of your game.
File Stream Access
As noted, your game should use the Content Pipeline to manage its game assets. There may be exceptional cases in which you'll want to manage your own game data—for example, if you have a preexisting content manager that you want to use with XNA Game Studio, or if you otherwise want stream level access to arbitrary game data files.
You can add arbitrary data files to title storage at build time, and they'll be available to your game during runtime through TitleContainer, which provides stream level access to any files in this location.
The following sections describe how to add files to, and read files from, title storage through file stream access:
User storage refers to the read-write storage supported by the game platform for saving information from the game at runtime. The data can be associated with a particular player's profile, or available to all players.
|On Windows Phone, writable user storage is handled by Silverlight, not by XNA Game Studio. For more information about saving game data on Windows Phone, see Writing Data (Windows Phone).|
Access User Storage
Accessing user storage is performed in two steps:
To access user storage
Retrieve a StorageDevice object to access the user storage device.
The BeginShowSelector method launches a dialog box that prompts the user to choose a storage device (for example, a memory unit or disk). This method works asynchronously, notifying the caller with an IAsyncResult or a callback method when the user has chosen a device.
For an example, see Getting a StorageDevice Asynchronously.
Access the user storage device to read or write.
Note Open the StorageContainer before reading file data. Not doing so will result in an InvalidOperationException exception.
For an example, see Saving Data to a Save Game File.
Implementation on Xbox 360
Changes to the files within a StorageContainer on Xbox 360 do not take effect until the StorageContainer is disposed. For this reason, only one StorageContainer can be open on the same device for the same profile.
Implementation on Windows
User storage is in the My Documents folder of the user who is currently logged in, in the SavedGames folder. A subfolder is created for each game according to the titleName passed to the BeginOpenContainer method. When no PlayerIndex is specified, content is saved in the AllPlayers folder. When a PlayerIndex is specified, the content is saved in the Player1, Player2, Player3, or Player4 folder, depending on which PlayerIndex was passed to BeginShowSelector.
Reading Game Data from Title Storage
Writing Data (Windows Phone)
Creating a File
Opening a File for Reading/Writing
Copying a File
Renaming a File
Enumerating Save Game Files
Deleting a Save Game File
Saving Data to a Save Game File
Getting a StorageDevice Asynchronously