简体   繁体   English

C#创建高分文本文件

[英]C# Create high score text file

I'm creating a little game (Memory) that should contain a high score list. 我正在创建一个应该包含高分列表的小游戏(Memory)。 The project is a windows grid app using XAML and C#. 该项目是一个使用XAML和C#的Windows网格应用程序。 This is the last solution I saw and it sounds logical to me: 这是我看到的最后一个解决方案,这对我来说听起来合乎逻辑:

private void New_Score(int score, string name)
{
    string filename = "highscore.txt";
    List<string> scoreList;
    if (StorageFile.Exists(filename))
        scoreList = File.ReadAllLines(filename).ToList();
    else
        scoreList = new List<string>();
    scoreList.Add(name + " " + score.ToString());
    var sortedScoreList = scoreList.OrderByDescending(ss => int.Parse(ss.Substring(ss.LastIndexOf(" ") + 1)));
    File.WriteAllLines(filename, sortedScoreList.ToArray());
}

If I try this in my window8 project. 如果我在window8项目中尝试这个。 The File class is not found. 找不到File类。

I'm searching for hours but I can't find a solution that fits for me. 我正在寻找几个小时,但我找不到适合我的解决方案。

The File class is in System.IO namespace. File类位于System.IO命名空间中。

Therefore, on top of your file, add this reference: 因此,在您的文件之上添加此引用:

using System.IO;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM