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

Posted

技术标签:

【中文标题】如何从文本文件中用逗号分隔行中的特定行和值【英文标题】:How to get specific line and value in line seperate with coma from text file 【发布时间】:2019-12-30 19:11:50 【问题描述】:

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

在资源/游戏 List.ini 中(从拖放中)

Apex Legends,Resource/Cover/Apex Legends.jpg,Resource/Game Info/Apex Legends.txt Fortnite,资源/封面/Fortnite.jpg,资源/游戏信息/Fortnite.txt PUBG,资源/封面/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

【问题讨论】:

【参考方案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 中选择一个游戏,然后单击一个按钮。您可以像这样从 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()

【讨论】:

谢谢先生,代码运行良好。我在vb net只是冒险,我是自学的,没有任何基本的编程并尝试学习它。再次感谢您 先生,我有一个问题,“目录名无效”也许这部分 Dim files As String() = Directory.GetFiles(gamePath.Path) 在带有扩展名的 .ini 文件路径中,我该怎么办 Tutorial: Learn to debug Visual Basic code using Visual Studio。调试并查看gamePath.Path 内部的内容。另见:First look at the Visual Studio Debugger 如果你在谷歌上搜索debug visual basic,你还会发现视频教程和更多关于这个主题的内容。

以上是关于如何从文本文件中用逗号分隔行中的特定行和值的主要内容,如果未能解决你的问题,请参考以下文章

从 xml 标签获取值到 unix 中的逗号分隔文本文件

用 TextField 中的小数分隔符替换逗号

如何将单个表从 phpmyadmin 导出到逗号分隔的文本文件?

将一张excel表中的数据全部粘贴到txt文本文件中,如何做

Python - 从文本文件中读取逗号分隔值,然后将结果输出到文本文件[关闭]

shell中的cut和paste函数,可以从多个文本中提取特定的列