我想让文本文件中的特定行出现在文本框中

时间:2019-05-11 17:31:32

标签: c#

我有一个.txt文件,其中包含多个喜欢的数据,例如:

Line 1 has - Name: Tom

Line 2 has - Score 1: 10

Line 3 has - Score 2: 5

Line 4 has - Score 3: 5

Line 5 has - Score 4: 7

Line 6 has - Score 5: 10

Line 7 has - Total Score: 37

Line 8 has - Name: Ben

Line 9 has - Score 1: 5

Line 10 has - Score 2: 10

Line 11 has - Score 3: 10

Line 12 has - Score 4: 0

Line 13 has - Score 5: 5

Line 14 has - Total Score: 30

我想做的是选择第1行,(Name: Tom)和第7行Total Score: 37)和 .txt文件中的第8行第14行。有什么方法可以选择.txt文件中的这些特定行,以便可以在文本框中显示它们?

我想要的结果是能够将选择的行放入文本框。任何帮助/建议将不胜感激。

2 个答案:

答案 0 :(得分:0)

我将创建一个类来存储名称和分数。这使您可以在列表中填写分数信息。

    class Player
    {
        public string Name { get; set; }
        public int Score { get; set; }

        public override string ToString()
        {
            return $"{Name,-15}: {Score,4}";
        }
    }

您可以枚举行并查看行是否以特定字符串开头。

const string path = @"C:\Users\Me\Documents\TheFile.txt";

var players = new List<Player>();
Player currentPlayer = null;
foreach (string line in File.ReadLines(path)) {
    if (line.StartsWith("Name:")) {
        // We have a new person. Create a `Player` object and add it to the list.
        currentPlayer = new Player { Name = line.Substring(6) };
        players.Add(currentPlayer);
    } else if (line.StartsWith("Total Score:") &&
        Int32.TryParse(line.Substring(13), out int score)) {

        // We have a score number. Convert it to `int` and assign it to the current player.
        currentPlayer.Score = score;
    }
}

请注意,Player是引用类型,因此currentPlayer引用列表中已经存在的同一播放器对象。这意味着currentPlayer.Score = score;也会更改列表中玩家的得分。

如果您覆盖ToString类的Player方法,则可以将播放器添加到ListBox中。然后,列表框将自动使用ToString来显示它们。这比文本框更加灵活,因为无论您有多少玩家,您始终只需要一个列表框。

listBox1.DataSouce = players; // Displays all the players with their scores.

答案 1 :(得分:0)

将数据解析为类。您可以获得名称和分数:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader(FILENAME);
            string line = "";
            Player newPlayer = null;
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.Length > 0)
                {
                    string[] splitLine = line.Split(new char[] { ':' }).ToArray();

                    if (line.StartsWith("Name"))
                    {
                        newPlayer = new Player();
                        Player.players.Add(newPlayer);

                        newPlayer.name = splitLine[1].Trim();
                    }
                    else
                    {
                        if (line.StartsWith("Score"))
                        {
                            if (newPlayer.scores == null) newPlayer.scores = new List<int>();
                            newPlayer.scores.Add(int.Parse(splitLine[1]));

                        }
                    }
                }
            }

            string[] playersNames = Player.players.Select(x => x.name).ToArray();

            Console.WriteLine(string.Join(" , ", playersNames));
            Console.ReadLine();
        }
    }
    public class Player
    {
        public static List<Player> players = new List<Player>();

        public string name { get; set; }
        public List<int> scores { get; set; }

    }
}