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

问题描述 投票:1回答:1

我想在所有行中获取值1以获取路径字符串,并以编程方式将覆盖物添加到flowlayoutpanel。对不起,我的英语不好,如果单词有误,请编辑。

在Resource / Game List.ini中(从拖放操作中得到]

Apex Legends,资源/封面/ Apex Legends.jpg,资源/游戏信息/ Apex Legends.txtFortnite,资源/封面/Fortnite.jpg,资源/游戏信息/Fortnite.txtPUBG,资源/封面/PUBG.jpg,资源/游戏信息/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
vb.net filereader streamreader programmatically-created
1个回答
1
投票

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

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,因此游戏名称将自动显示在列表框中。

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

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语法在逗号处分割每一行并创建游戏路径信息。

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

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

然后仅读取一次文件并将结果分配给变量

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

获取文件数

Dim fileCount As Integer = files.Count

整个Click方法:

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()
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.