Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Writing Data (Windows Phone)

XNA Game Studio 4.0

XNA Game Studio 4.0 does not provide access to writeable storage on Windows Phone. To access such storage, you'll need to use classes from the System.IO.IsolatedStorage namespace.

For Windows Phone projects, Visual Studio automatically adds the assembly containing System.IO.IsolatedStorage to your project. There is no need to add any additional references to your project.

Complete Sample

The code in this 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.

To save game data with System.IO.IsolatedStorage

  1. Add a using System.IO.IsolatedStorage statement at the beginning of the source file in which you'll be using the namespace.

    using System.IO.IsolatedStorage;
    
  2. Use IsolatedStorageFile.GetUserStoreForApplication to get an IsolatedStorageFile object that can be used to create files and directories, or to read and write existing files.

    Ff604992.note(en-us,XNAGameStudio.40).gifNote

    When IsolatedStorageFile.GetUserStoreForApplication is called within an XNA Game Studio game for Windows (but not for Xbox 360 or Windows Phone), an InvalidOperationException will result. To avoid this exception, use the GetUserStoreForDomain method instead.

    A cross-platform game can use the conditional compilation symbols to control which method is called.

    
    #if WINDOWS
        IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForDomain();
    #else
        IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();
    #endif    
              
    
  3. Use IsolatedStorageFile.OpenFile to open a file for writing, and use the returned IsolatedStorageFileStream to write data to the file.

    protected override void OnExiting(object sender, System.EventArgs args)
    {
        // Save the game state (in this case, the typed text).
        IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();
    
        // open isolated storage, and write the savefile.
        IsolatedStorageFileStream fs = null;
        fs = savegameStorage.OpenFile(SAVEFILENAME, System.IO.FileMode.Create);
        if (fs != null)
        {
            // just overwrite the existing info for this example.
            fs.WriteByte(gameState); // a single byte
            if ((typedText != null) && (typedText.Length > 0))
            {
                fs.Write(System.Text.Encoding.UTF8.GetBytes(typedText), 0, typedText.Length);
            }
            fs.Close();
        }
    
        base.OnExiting(sender, args);
    }
    

Reading saved data written in this way uses a very similar process. For example:

protected override void Initialize()
{
    gameState = 0;

    IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();

    // open isolated storage, and write the savefile.
    if(savegameStorage.FileExists(SAVEFILENAME))
    {
        IsolatedStorageFileStream fs = null;
        try
        {
            fs = savegameStorage.OpenFile(SAVEFILENAME, System.IO.FileMode.Open);
        }
        catch (IsolatedStorageException e)
        {
            // The file couldn't be opened, even though it's there.
            // You can use this knowledge to display an error message
            // for the user (beyond the scope of this example).
        }

        if (fs != null)
        {
            // Reload the last state of the game.  This consists of the UI mode
            // and any text that was typed on the keyboard input screen. 
            byte[] saveBytes = new byte[256];
            int count = fs.Read(saveBytes, 0, 256);
            if (count > 0)
            {
                // the first byte is the mode, the rest is the string.
                gameState = saveBytes[0];
                if (count > 1)
                {
                    typedText = System.Text.Encoding.UTF8.GetString(saveBytes, 1, count - 1);
                }
                fs.Close();
            }
        }
    }

    base.Initialize();
}
Ff604992.note(en-us,XNAGameStudio.40).gifNote
IsolatedStorageFile provides many more methods than are shown here, including support for asynchronous reads and writes. For complete documentation, see Isolated Storage for Windows Phone in the Windows Phone Development documentation, and the IsolatedStorage reference on MSDN.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.