Vb.net 如何显示文本文件?

Vb.net 如何显示文本文件?,vb.net,text-files,Vb.net,Text Files,我有一个程序,允许用户完成测试,所有用户的分数都记录在一个单独的文本文件中。如何显示每个文本文件,以便显示每个测试的分数?您可以通过Linq查询来实现这一点: var scores = from file in System.IO.Directory.GetFiles(@"c:\path", "*.scores") select GetScoreFromFile(System.IO.File.ReadAllLines(file)); 使用GetScoreFromFil

我有一个程序,允许用户完成测试,所有用户的分数都记录在一个单独的文本文件中。如何显示每个文本文件,以便显示每个测试的分数?

您可以通过Linq查询来实现这一点:

var scores = from file in System.IO.Directory.GetFiles(@"c:\path", "*.scores")
             select GetScoreFromFile(System.IO.File.ReadAllLines(file));
使用
GetScoreFromFile

private int GetScoreFromFile(string[] scoreLines)
{
    // Some code to read the score from the file
}

要实际显示分数。。。你可能想做些新奇的东西(听起来很像游戏)。如果没有,请使用某种网格控件。

您可以使用ini配置文件来执行此操作。我设计了一个VB类库,其中包含一个ini文件读取器类,可用于读取和写入ini配置文件。您不必将每个分数保存为一个单独的文件,而是可以通过将它们分段来将其包含在单个ini配置文件中

你可以下载我的类库

在项目中添加对库的引用

下面是一个如何使用它的示例:

Dim ScoresReader As New Rodit.IniLib.IniParser

Public Sub ReadTestScores(ScoreFilePath As String)
    ScoresReader = New Rodit.IniLib.IniParser 'Initializes new instance of class
    ScoresReader.Load(ScoreFilePath) 'Loads scores file specified in subroutine args
    For Each score As Rodit.IniLib.IniParser.IniSection In ScoresReader.Sections 'Each section in the ini file will represent a different user's score
        'Here you can write whatever you want to do with the user's score
    Next
End Sub

'Call example:
Protected Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
    ReadTestScores("C:\scores.ini")
End Sub
Ini文件结构:

[INIFILETEST] <-- Section
Name=Rodit
 ^     ^
 |     |
Key   Value
[INIFILETEST]