Export (0) Print
Expand All

How To: Load a Game Data File

This example demonstrates how to use the File class to read a file from the game image directory.

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 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
/// ship.dds has been deployed alongside the game.
/// </summary>
private static void DoOpenFile()
    FileStream file = OpenTitleFile(
        "ship.dds", 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

© 2015 Microsoft