xna windows phone中的文件I / O ....读取和写入文本数据

时间:2013-10-21 09:59:52

标签: c# windows-phone

我想增加玩家的分数并检查高分,以便我将分数存储在文本文件中。我正在尝试下面的代码,但它正在抛出异常。

例外:价值不在预期范围内。

public void storage()
{

    var appstorage = IsolatedStorageFile.GetUserStoreForApplication();
    String filename = "store.txt";
    using (var file = appstorage.OpenFile(filename, FileMode.OpenOrCreate,FileAccess.ReadWrite))
    {

        using (var writer = new StreamWriter(file))
        {
            writer.Write(score);
        }

        using(var reader=new StreamReader(file)) 
        {
            playerscore =reader.ReadLine();

        }


    }
}

1 个答案:

答案 0 :(得分:0)

我真的不确定Windows手机支持,但在Windows中,我会做这样的事情

using (var file = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    using (var writer = new BinaryWriter(file))
    {
       writer.Write(score);
    }

    using (var reader = new BinaryReader(file))
    {
        playerscore = reader.ReadInt32();
     }
}

当然,如果你只能阅读写文本,这就是解决方案

using (var file = File.Open(filename, FileMode.OpenOrCreate,` FileAccess.ReadWrite))
{
    using (var writer = new StreamWriter(file))
    {
        writer.WriteLine(score.ToString());
    }

    using (var reader = new StreamReader(file))
    {
        playerscore = int.Parse(reader.ReadLine());

    }
}