If you passed a player index to the ShowStorageDeviceGuide
method, the AllPlayers folder would be replaced with PlayerOne,
PlayerTwo, etc.
The StorageContainer class has only four properties that you’ll
really need to know about and no methods:
Table 6-2
Method/Property Description
Path Gets the path to the location of the user’s save game files.
StorageDevice Gets the StorageDevice that holds the files in this container.
TitleLocation Gets the path to the location of the application.
TitleName Get the name of the title.
Once you have the StorageContainer object you can use it, along
with the normal .NET Framework I/O objects, to do your reading
and writing of data.
Standard I/O Files and Operations
Although there are many different types of files,
when creating a game you’ll probably use only
three or four — plain text, XML (which is just
formatted plain text), binary, and custom file
types created by you for your game. The .NET
Framework automatically knows how to handle
the first three and has functionality to enable
you to handle your own file types.
To work with these files, the .NET Frame
work provides a good number of classes, as you
can see in Figure 6-2.
A complete examination of this namespace is
beyond the scope of this book. We’ll take a look
at just a couple of classes that we’ll actually use
a little later on and in future chapters.
Before we can work with our files, we have
to find them. Once we find them, we have to
open them for reading or writing. This is where
206 Chapter 6
Figure 6-2
the File class comes in. It’s made up of static methods so you don’t
create an instance of the class. When we want to see if a file exists
in our storage space we use the following:
Listing 6-3
if (File.Exists(sc.Path + "\\data.txt"))
//open the file here
Note that in C#, like in C++, you need to use double backslashes
for paths. In C#, you can also use @"data.txt" . Once we know the
file exists, we need to open and read it. What we use to read it
depends on what type of file it is. We’ll start with a plain text file
that contains the text “your world. your game.” The following reads
the contents of the file into a string variable:
Listing 6-4
if (File.Exists(sc.Path + "\\data.txt"))
StreamReader sr = new StreamReader(sc.Path + "\\data.txt");
string data = sr.ReadLine();
If the file contained more than one line, we would simply use one of
the other methods of the StreamReader object:
Table 6-3
Method Description
Read Overloaded. Overridden. Reads the next character or next set of
characters from the input stream.
ReadBlock Reads a maximum of count characters from the current stream
and writes the data to buffer, beginning at index. (Inherited from
ReadLine Overridden. Reads a line of characters from the current stream
and returns the data as a string.
ReadToEnd Overridden. Reads the stream from the current position to the end
of the stream.
I/O 207
Okay, so we’ve read in our data file, but let’s say the player has
made some changes and now we need to save it. It’s almost the
same operation:
Listing 6-5
string data="my game. my world.";
if (File.Exists(sc.Path + "\\data.txt"))
StreamWriter sw = new StreamWriter(new FileStream(container.Path +
The other file format you’ll probably want to consider using is a
binary data file. You can probably guess that the classes you’ll use to
handle these files are the BinaryReader and BinaryWriter classes.
They work exactly the same way as the two previous classes:
Listing 6-6
FileStream fs = new FileStream(sc.Path + "\\data.dat", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
string data="my game. my world.";
if (File.Exists(container.Path + "\\data.dat"))
BinaryReader br = new BinaryReader(new FileStream(container.Path +
data = br.ReadString();
Just like the StreamWriter class, there are methods for reading just
about every data type, and the Write method is overloaded for just
about every data type as well. If you open the file, you’ll notice that the
text is still readable. But what if we don’t want the player to easily
know what data is in a file? In that case we’ll need to obfuscate the text
208 Chapter 6

Get Essential XNA Game Studio 2.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.