0

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

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

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();

        }


    }
}
4

1 回答 1

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());

    }
}
于 2013-10-21T10:23:09.287 回答