繁体   English   中英

C#从日志文件读取特定属性

[英]C# Read specific property from a log file

在我的MUGEN锦标赛程序中,我想处理游戏创建的日志文件中的比赛结果。 日志如下所示:

[Match 1]
totalmatches = 1
team1.1 = 
team2.1 = 
stage = stages/kowloon.def

[Match 1 Round 1]
winningteam = 1
timeleft = -1.00
p1.name = Knuckles the Echidna
p1.life = 269
p1.power = 0
p2.name = Shadow the Hedgehog
p2.life = 0
p2.power = 2684

[Match 1 Round 2]
winningteam = 2
timeleft = -1.00
p1.name = Knuckles the Echidna
p1.life = 0
p1.power = 1510
p2.name = Shadow the Hedgehog
p2.life = 586
p2.power = 2967

[Match 1 Round 3]
winningteam = 2
timeleft = -1.00
p1.name = Knuckles the Echidna
p1.life = 0
p1.power = 3000
p2.name = Shadow the Hedgehog
p2.life = 789
p2.power = 777

我想要的是处理最后的winningteam属性,以确定比赛的结果。 最有效的方法是什么? (也许使用LINQ)

您可以使用File.ReadLines返回作为IEnumerable<string>的行的枚举。

// string path contains file path and file name.
string line = File.ReadLines(path)
    .LastOrDefault(ln => ln.StartsWith("winningteam ="));

现在在=处分割字符串,修剪第二部分,然后将团队编号作为字符串

if (line != null) {
    string team = line.Split('=')[1].Trim();
    // With "winningteam = 2", [0] = "winningteam ", [1] = " 2"

    // Optionally convert it to a number
    int teamNo = Int32.Parse(team);

    ...
}

暂无
暂无

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

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