我目前正在使用以下内容将目录中所有文本文件的内容读入数组
Dim allLines() As String = File.ReadAllLines(txtfi.FullName)
在文本文件中只有6行,它们都遵循相同的格式,并且会读取类似
的内容forecolour =黑
我正在尝试搜索“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=
出于这个数组的目的,我只需要unitname,unitcode&操作。继续我将需要日期计划,因为这是工作时我想尝试如何只显示日期规划与datepicker日期匹配的信息。希望能有所帮助,感谢任何指导或提示
答案 0 :(得分: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删除每个
的显示内容。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),则可以真正简化此代码