Reading Game Data from Title Storage
Describes how to load game data from the title storage location.
Tip
This topic describes how to load files from title storage that the game will access through stream I/O. This is the exceptional case, and is not recommended for most XNA Game Studio games. Instead, you should consider using the Content Pipeline to manage your game data most efficiently.
Complete Sample
The code in the topic shows you the technique for loading game data. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.
Download StorageDemo_Sample.zip
Loading a Game Data File from Title Storage
You can use OpenStream to load game data files from title storage to get read-only stream access to the files in this location. The game data files must already exist. See Adding Game Data Files to Title Storage for information about adding game data files to your project to have them accessible at runtime.
To read a game data file with TitleContainer
This procedure describes how to access game data files once they are added to your project.
Pass the name of the file to read to TitleContainer.OpenStream.
This returns a System.IO.Stream with read-only access to the file.
Construct a new System.IO.StreamReader, passing it the Stream obtained from TitleContainer.OpenStream
/// <summary>
/// This method opens a file using System.IO classes and the
/// TitleLocation property. It presumes that a file named
/// ship.dds has been deployed alongside the game.
/// </summary>
private static void DoOpenFile()
{
try
{
System.IO.Stream stream = TitleContainer.OpenStream("ship.dds");
System.IO.StreamReader sreader = new System.IO.StreamReader(stream);
// use StreamReader.ReadLine or other methods to read the file data
Console.WriteLine("File Size: " + stream.Length);
stream.Close();
}
catch (System.IO.FileNotFoundException)
{
// this will be thrown by OpenStream if gamedata.txt
// doesn't exist in the title storage location
}
}