简体   繁体   中英

C# Create high score text file

I'm creating a little game (Memory) that should contain a high score list. The project is a windows grid app using XAML and C#. 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. The File class is not found.

I'm searching for hours but I can't find a solution that fits for me.

The File class is in System.IO namespace.

Therefore, on top of your file, add this reference:

using System.IO;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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