Saving data to file on windows 8

Hi everyone. I have a question about saving data to files on Windows 8.1.
I tried to make it work with Isolated Storage, but with no luck.
I have read something that on windows 8 you should save the asynchronous way.
My question is: if i can solve the file saving the async way, then can my game save on windows 7 too? or only windows 8??

Or if someone knows a way to save some simple data to a file that would be appreciated.
Or maybe is there a way to connect to my website and save/load highscore data from the database?

Thanks in advance, Ryeki

I have a basic implementation of IsolatedStorage for Windows 8 that I have successfully used on a released title. The project used the same code on Windows 7 desktop and Windows 8 Modern.

1 Like

Thanks. I’m gonna try implementing it later after work. I have something similar from an older project, but ill try this too.
Thanks again, i will check in later with the results.
Ryeki

I just got isolated storage to work today. I’ve only been programming in C# for a week or two so I felt pretty good.

This tutorial makes it ridiculously easy to understand:

I think its pretty clear it will only work for windows 8 and windows phone 8 apps though. I struggled with other solutions especially ones that have lines that begin with Read., couldn’t ever get visual studio 2013 to recognize Read.

Hi Ryeki!

At first saving and reading data on Windows 8 can seem difficult, but once you figure it out; it’s very trivial.

All you need is…

using Windows.Storage;
using Windows.Storage.Streams;

then, within a method use the async keyword like so…

public static async void GetHighScores() { }

//then, inside that method, grab a storage folder like so…

StorageFolder localFolder = ApplicationData.Current.LocalFolder;

//then, inside a try/catch, grab a storage file like so…

try
{
StorageFile highScoreFile = await localFolder.GetFileAsync(“HighScores”);

//after that I used a Stream and a BinaryReader to read in the data like so…

using(Stream stream = await highScoreFile.OpenStreamForReadAsync())
{
using(BinaryReader reader = new BinaryReader(stream))
{
//iterate through whatever you saved and want read back in/or you have to figure out the length of you stream, but in my case I know I saved 10 high-scores.
for(int i = 0; i < 10; i++)
{
//I’m adding my player’s names and score to a list, so I read them in like this
Player.name.Add(reader.ReadString());
Player.score.Add(reader.ReadInt32());
}
}
}
catch { }

//writing to the file is like so…
public static async void SaveHighScores()
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile highScoreFile = await localFolder.CreateFileAsync(“HighScores”, CreationCollisionOption.ReplaceExisting);

using(Stream stream = await highScoreFile.OpenStreamForWriteAsync())
{
using(BinaryWriter writer = new BinaryWriter(stream))
{
for(int i = 0; i < 10; i++)
{
writer.Write(Player.name[i]);
writer.Write(Player.score[i]);
}
writer.Flush();
}
}

Hopefully this helps…

1 Like

Hi. Thanks for the answers. I got it to work with the same code that i used on windows 7:

using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;

namespace TankFormer.Utils
{
    public class HighScore
    {
        List<int> scores;
        int score;
        string filename;

        public HighScore()
        {
            scores = new List<int>(10);
            filename = "gamedata.txt";
            score = 0;

            for (int i = 0; i < 10; i++)
            {

                scores.Add(0);
            }

        }


        public HighScore(int _score)
        {

            score = 0;
            scores = new List<int>(10);
            filename = "gamedata.txt";

            for (int i = 0; i < 10; i++)
            {
                scores.Add(0);
            }
        }


        public int Score
        {
            get { return score; }
            set { score = value; }
        }

        public List<int> Scores
        {
            get { return scores; }
        }


        public void SaveHighScore()
        {

            if (score > scores[9])
                scores[9] = score;

            scores.Sort();
            scores.Reverse();
            //Array.Sort(scores);
            //Array.Reverse(scores);

            IsolatedStorageFile highScoreStorage = IsolatedStorageFile.GetUserStoreForDomain();


            IsolatedStorageFileStream userDataFile =
              new IsolatedStorageFileStream(filename, FileMode.OpenOrCreate, highScoreStorage);

            // create a writer to the stream...
            StreamWriter writeStream = new StreamWriter(userDataFile);

            // write strings to the Isolated Storage file...
            for (int i = 0; i < 10; i++)
            {

                writeStream.WriteLine(scores[i].ToString());
            }


            // Tidy up by flushing the stream buffer and then closing
            // the streams...
            writeStream.Flush();
            writeStream.Close();
            userDataFile.Close();
        }

        public void LoadHighScore()
        {
            if (System.IO.File.Exists(filename))
            {
                IsolatedStorageFile highScoreStorage = IsolatedStorageFile.GetUserStoreForDomain();


                if (highScoreStorage.FileExists(filename))
                {
                    IsolatedStorageFileStream userDataFile =
                    new IsolatedStorageFileStream(filename, FileMode.Open, highScoreStorage);

                    // create a reader to the stream...
                    StreamReader readStream = new StreamReader(userDataFile);

                    for (int i = 0; i < 10; i++)
                    {

                        scores[i] = Convert.ToInt32(readStream.ReadLine());
                    }

                    scores.Sort();
                    scores.Reverse();
                    //Array.Sort(scores);
                    //Array.Reverse(scores);

                    // Tidy up by closing the streams...
                    readStream.Close();
                    userDataFile.Close();
                }
                else
                {
                    SaveHighScore();
                }
            }
            else
            {
                System.IO.File.Create(filename);

                SaveHighScore();
            }

        }
    }
}

Thanks!
Ryeki

Hi Ryeki!

Good! Thanks for posting your solution for future reference.