如何从文本文件中获取以逗号分隔的特定行和值

How to get specific line and value in line seperate with coma from text file

我想在获取路径字符串的所有行中获取值 1,并以编程方式将封面添加到 flowlayoutpanel。

in Resource/Game List.ini(来自拖放)

Apex Legends,Resource/Cover/Apex Legends.jpg,Resource/Game Info/Apex Legends.txt
Fortnite,Resource/Cover/Fortnite.jpg,Resource/Game Info/Fortnite.txt
PUBG,Resource/Cover/PUBG.jpg,Resource/Game Info/PUBG.txt

这是我的代码:

Private Sub LabelSetting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelSetting.Click
        FlpAddItem.Controls.Clear()

        'I am confused in this part to get value 1 in all line for get path string
        'Directory.GetFiles(Path) will be replace with streamreader from lines(i) value 1

        Dim Path = '???
        Dim ImageX As Image = Nothing
        Dim x As Int32 = Directory.GetFiles(Path).Count - 1
        Dim Img(x) As PictureBox
        Dim ImgText(x) As Label
        Dim ImgPanel As Panel

        For i = 0 To Directory.GetFiles(Path).Count - 1
            ImgPanel = New Panel
            With ImgPanel
                .Width = 96
                .Height = 136
                .BackColor = Color.Transparent
            End With

            FlpAddItem.Controls.Add(ImgPanel) 'Add panel to the flowlayoutpanel

            ImgText(i) = New Label
            With ImgText(i)
                .Name = Directory.GetFiles(Path)(i).Replace(Path, "").Replace(".jpg", "").Replace(".png", "")
                .FlatStyle = FlatStyle.Popup
                .Width = 116
                .Height = 40
                .Padding = New Padding(0, 3, 0, 0)
                .TextAlign = ContentAlignment.TopCenter
                .Dock = DockStyle.Bottom
                .BackColor = Color.Transparent
                .ForeColor = Color.Black
            End With

            Img(i) = New PictureBox
            With Img(i)
                .Width = 96
                .Height = 96
                .Padding = New Padding(20, 20, 20, 20)
                .BackColor = Color.Transparent
                .BorderStyle = BorderStyle.FixedSingle
                .SizeMode = PictureBoxSizeMode.StretchImage
            End With
            ImgPanel.Controls.Add(Img(i)) 'Add the picturebox to the panel

            ImageX = Image.FromFile(Directory.GetFiles(Path)(i), True)
            Img(i).Image = Image.FromFile(Directory.GetFiles(Path)(i))
            ImgText(i).Text = Directory.GetFiles(Path)(i)
            ImgPanel.Controls.Add(ImgText(i))
        Next
End Sub

我建议为游戏名称和路径信息创建一个class

Public Class GamePath
    Public Property GameName As String
    Property Path As String

    Public Overrides Function ToString() As String
        Return GameName
    End Function
End Class

我已经重写了ToString,这样游戏名称会自动显示在一个ListBox中。

加载表单时,我从 INI 文件中读取此信息并将其设置为列表框的数据源,您可以在其中 select 游戏。

Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim games =
        From line In File.ReadLines(IniFilePath)
        Let parts = line.Split(","c)
        Select New GamePath With {.GameName = parts(0), .Path = parts(1)}
    GameListBox.DataSource = games.ToList()
    GameListBox.SelectedIndex = 0 'Select first game
End Sub

请注意,使用 File.ReadLines 比使用 StreamReader 更容易。您必须将 Imports System.IO 添加到代码的顶部。然后我们使用 LINQ 语法以逗号分隔每一行并创建游戏路径信息。

用户select在列表框中选择了一个游戏,然后单击了一个按钮。您可以像这样从 ListBox 中获取文件路径信息:

Dim gamePath As GamePath = DirectCast(GameListBox.SelectedItem, GamePath)

然后只读取一次文件并将结果赋给一个变量

Dim files As String() = Directory.GetFiles(gamePath.Path)

获取文件数

Dim fileCount As Integer = files.Count

整个点击方法:

Private Sub StartGameButton_Click(sender As Object, e As EventArgs) Handles StartGameButton.Click
    FlpAddItem.Controls.Clear()

    Dim gamePath As GamePath = DirectCast(GameListBox.SelectedItem, GamePath)
    Dim files As String() = Directory.GetFiles(gamePath.Path)
    Dim fileCount As Integer = files.Count

    Dim ImageX As Image = Nothing
    Dim Img(fileCount) As PictureBox
    Dim ImgText(fileCount) As Label
    Dim ImgPanel As Panel

    For i = 0 To fileCount - 1
        Dim filePath = files(i)
        ImgPanel = New Panel
        With ImgPanel
            .Width = 96
            .Height = 136
            .BackColor = Color.Transparent
        End With

        FlpAddItem.Controls.Add(ImgPanel) 'Add panel to the flowlayoutpanel

        ImgText(i) = New Label
        With ImgText(i)
            .Name = System.IO.Path.GetFileNameWithoutExtension(filePath)
            .FlatStyle = FlatStyle.Popup
            .Width = 116
            .Height = 40
            .Padding = New Padding(0, 3, 0, 0)
            .TextAlign = ContentAlignment.TopCenter
            .Dock = DockStyle.Bottom
            .BackColor = Color.Transparent
            .ForeColor = Color.Black
        End With

        Img(i) = New PictureBox
        With Img(i)
            .Width = 96
            .Height = 96
            .Padding = New Padding(20, 20, 20, 20)
            .BackColor = Color.Transparent
            .BorderStyle = BorderStyle.FixedSingle
            .SizeMode = PictureBoxSizeMode.StretchImage
        End With
        ImgPanel.Controls.Add(Img(i)) 'Add the picturebox to the panel

        ImageX = Image.FromFile(filePath, True)
        Img(i).Image = Image.FromFile(filePath)
        ImgText(i).Text = filePath
        ImgPanel.Controls.Add(ImgText(i))
    Next
End Sub

一些细节:

在 For 循环中,您可以使用

获取图像文件的路径
Dim filePath = files(i)

您可以通过

获取图片的名称
.Name = System.IO.Path.GetFileNameWithoutExtension(filePath)

这会自动删除目录名称和扩展名。

以后不要再打Directory.GetFiles了:

ImageX = Image.FromFile(filePath, True)
Img(i).Image = Image.FromFile(filePath)
ImgText(i).Text = filePath

如果你只想将文件路径读入列表,你可以写

Dim games =
    (From line In File.ReadLines(IniFilePath)
     Select line.Split(","c)(1)).ToList()