【问题标题】:Search line in text file and return value from a set starting point vb.net在文本文件中搜索行并从设定的起点返回值 vb.net
【发布时间】:2013-11-19 21:17:04
【问题描述】:

我目前正在使用以下方法将目录中所有文本文件的内容读入数组中

 Dim allLines() As String = File.ReadAllLines(txtfi.FullName)

在文本文件中只有 6 行,它们都遵循相同的格式,并且会读到类似

前色=黑色

我正在尝试搜索单词“forecolour”并检索“=”符号(黑色)之后的信息,以便我可以填充以下代码

AllDetail(numfiles).uPath = ' this needs to be the above result

我只发布了部分代码,但如果有帮助,我可以发布其余部分。如果可能的话,我只需要一点指导

谢谢

这是完整的代码

Dim numfiles As Integer

    ReDim AllDetail(0 To 0)
    numfiles = 0
    lb1.Items.Clear()

    Dim lynxin As New IO.DirectoryInfo(zMailbox)

    lb1.Items.Clear()

    For Each txtfi In lynxin.GetFiles("*.txt")

        Dim allLines() As String = File.ReadAllLines(txtfi.FullName)


        ReDim Preserve AllDetail(0 To numfiles)

        AllDetail(numfiles).uPath = 'Needs to be populated
        AllDetail(numfiles).uName = 'Needs to be populated
        AllDetail(numfiles).uCode = 'Needs to be populated
        AllDetail(numfiles).uOps =  'Needs to be populated

        lb1.Items.Add(IO.Path.GetFileNameWithoutExtension(txtfi.Name))
        numfiles = numfiles + 1



    Next


End Sub

AllDetail(numfiles).uPath = Would be the actual file path
    AllDetail(numfiles).uName = Would be the detail after “unitname=”
    AllDetail(numfiles).uCode = Would be the detail after “unitcode=”
    AllDetail(numfiles).uOps =  Would be the detail after “operation=”

在正在读取的文本文件中会有以下几行

Unitname=
Unitcode=
Operation=
Requirements=
Dateplanned=

对于这个数组,我只需要单元名称、单元代码和操作。展望未来,我将需要 dateplanned,因为当它工作时,我想尝试弄清楚如何仅在 dateplanned 与 datepicker 中的日期匹配时才显示信息。希望能得到帮助和任何指导或提示

【问题讨论】:

  • 我不确定我是否理解您的问题。不清楚

标签: vb.net winforms visual-studio-2012


【解决方案1】:

如果你的文件不是很大,你可以简单地

Dim allLines() As String = File.ReadAllLines(txtfi.FullName)
For each line in allLines
   Dim parts = line.Split("="c)
   if parts.Length = 2 andalso parts(0) = "unitname" Then
       AllDetails(numFiles).uName = parts(1)
       Exit For
   End If

下一个

如果您完全确定输入文件的格式,您还可以使用 Linq 删除每个文件的 explict

Dim line = allLines.Where(Function(x) (x.StartsWith("unitname"))).SingleOrDefault()
if line IsNot Nothing then
    AllDetails(numFiles).uName = line.Split("="c)(1)
End If

编辑

查看添加到您的问题的最后一个细节,我认为您可以通过这种方式重写您的代码,但仍然缺少一条关键信息。

应该在数组AllDetails中存储什么样的对象?

我想你有一个名为 FileDetail 的类

Public class FileDetail
    Public Dim uName As String
    Public Dim uCode As String
    Public Dim uCode As String
End Class

....

numfiles = 0
lb1.Items.Clear()

Dim lynxin As New IO.DirectoryInfo(zMailbox)

' Get the FileInfo array here and dimension the array for the size required
Dim allfiles = lynxin.GetFiles("*.txt")

' The array should contains elements of a class that have the appropriate properties
Dim AllDetails(allfiles.Count) as FileDetail

lb1.Items.Clear()

For Each txtfi In allfiles)
    Dim allLines() As String = File.ReadAllLines(txtfi.FullName)

    AllDetails(numFiles) = new FileDetail()
    AllDetails(numFiles).uPath = txtfi.FullName

    Dim line = allLines.Where(Function(x) (x.StartsWith("unitname="))).SingleOrDefault()
    if line IsNot Nothing then
        AllDetails(numFiles).uName = line.Split("="c)(1)
    End If

    line = allLines.Where(Function(x) (x.StartsWith("unitcode="))).SingleOrDefault()
    if line IsNot Nothing then
        AllDetails(numFiles).uName = line.Split("="c)(1)
    End If

    line = allLines.Where(Function(x) (x.StartsWith("operation="))).SingleOrDefault()
    if line IsNot Nothing then
        AllDetails(numFiles).uOps = line.Split("="c)(1)
    End If

    lb1.Items.Add(IO.Path.GetFileNameWithoutExtension(txtfi.Name))
    numfiles = numfiles + 1
Next

请记住,如果您开始使用 List(Of FileDetails),则此代码可以真正简化

【讨论】:

  • 感谢您的回答。我在使用 AllDetails(numFiles) = line.Split("="c)(1) 时遇到问题。 line.split 部分不能作为数组字符串的一部分工作?!。我已将我正在使用的完整代码添加到原始帖子中,以防我错过了一些细节
  • 您还需要说明您希望在 AllDetails 的其他字段中插入的内容。还要说明 AllDetails 数组中应该存储的对象是什么
  • 我已添加到原帖中 - 非常感谢您的帮助
  • 非常感谢,这对我来说非常有用。我的 .net 学习曲线陡峭,我的经理对 VB6 很熟悉,但对 .Net 不太熟悉。我读过 List(of FileDetails) 所以我想我会看看我是否可以在某个时候转换
猜你喜欢
  • 1970-01-01
  • 2021-08-17
  • 2019-05-24
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多