Partager via


Writing Data (Windows Phone)

XNA Game Studio 4.0 Refresh 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.

Download MouseInput

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.

    Note

    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.

  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 high score).
    #if WINDOWS_PHONE
                IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();
    #else
                IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForDomain();
    #endif
    
                // open isolated storage, and write the savefile.
                IsolatedStorageFileStream fs = null;
                using (fs = savegameStorage.CreateFile(SAVEFILENAME))
                {
                    if (fs != null)
                    {
                        // just overwrite the existing info for this example.
                        byte[] bytes = System.BitConverter.GetBytes(highScore);
                        fs.Write(bytes, 0, bytes.Length);
                    }
                }
    
                base.OnExiting(sender, args);
            }
    

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

        protected override void Initialize()
        {
            // open isolated storage, and load data from the savefile if it exists.
#if WINDOWS_PHONE
            using (IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication())
#else
            using (IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForDomain())
#endif
            {
                if (savegameStorage.FileExists(SAVEFILENAME))
                {
                    using (IsolatedStorageFileStream fs = savegameStorage.OpenFile(SAVEFILENAME, System.IO.FileMode.Open))
                    {
                        if (fs != null)
                        {
                            // Reload the saved high-score data.
                            byte[] saveBytes = new byte[4];
                            int count = fs.Read(saveBytes, 0, 4);
                            if (count > 0)
                            {
                                highScore = System.BitConverter.ToInt32(saveBytes, 0);
                            }
                        }
                    }
                }
            }
            base.Initialize();
        }

Note

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.

See Also

Concepts

What Is Storage?

Tasks

Handling Interruptions on Windows Phone