How To: Load a Game Data File

Demonstrates how to use the File class to read a file from the game image directory.

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.

Loading a Game Data File

To load a game data file

  1. Construct the full path to the file by combining StorageContainer.TitleLocation with the file name.

  2. Call File.Open with the full file path to get a FileStream containing the file data.

/// <summary>
/// This method opens a file using System.IO classes and the
/// TitleLocation property.  It presumes that a file named
/// has been deployed alongside the game.
/// </summary>
private static void DoOpenFile()
    FileStream file = OpenTitleFile(
        "", FileMode.Open, FileAccess.Read);
    Console.WriteLine("File Size: " + file.Length);
private static FileStream OpenTitleFile(
    string filename, FileMode mode, FileAccess access)
    string fullpath = Path.Combine(StorageContainer.TitleLocation, filename);
    return File.Open(fullpath, mode, access);

Community Additions