从记事本读取特定数据

时间:2019-05-21 07:37:10

标签: vb.net notepad

我有一个记事本,其中包含一些未加密的数据,如下所示,我想从该文本文件中读取数据并将数据填充到各个文本框中。

这是我的输入的样子:

========================================================
Modified Date : 5/20/2019 8:45:56 AM     Modified by : 123
ID : 18677544
OLD Values:

First Name        Last Name        Middle Initial    
--------------    --------------  -----------------
John              Humpty        


NEW Values:

First Name        Last Name        Middle Initial    
--------------    --------------  -----------------
George            Louis 

========================================================

这是我的代码

Dim path As String = "C:\Users\XXX\Desktop\123.txt"
    Dim searchTarget = "Modified Date"
     For Each line In File.ReadAllLines(path)
        If line.Contains(searchTarget) Then ' found it!
          Dim toBeSearched As String = "Modified Date : "
          Dim code As String = line.Substring(line.IndexOf(toBeSearched) + toBeSearched.Length)
          txtModifiedDate.Text = code// Here I'm getting Modified By value also but I need only the Modified Date in this textbox similarly for others
     Exit For ' then stop
    End If
Next line

根据宝藏获得过载异常错误

enter image description here

已更新:

enter image description here

1 个答案:

答案 0 :(得分:2)

如果您提供的示例文本文件始终是它的外观,则应该可以。

我对大多数值进行了硬编码,但是对于上面的文本文件,以下代码有效:

Sub GetInfo()
    Dim path As String = "C:\Users\XXX\Desktop\123.txt"
    Dim lines As New List(Of String)
    lines.AddRange(IO.File.ReadAllLines(path))

    Dim tmpArray = lines(1).Split(" "c)

   '******Update*******************
    Dim tmplist As New List(Of String)
    tmplist.Add(tmpArray(3))
    tmplist.Add(tmpArray(4))
    tmplist.Add(tmpArray(5))

    Dim strModifiedDate As String = String.Join(" ", tmplist.ToArray) 'Get the date by joining the date, time

    '************************ 
    strModifiedDate.Text = strModifiedDate

    Dim strModifiedBy As String = tmpArray(UBound(tmpArray))
    strModifiedBy.Text = strModifiedBy

    tmpArray = lines(2).Split(":"c)
    strID.Text = tmpArray(1).Trim

    Dim tmpStr As String = lines(7).Split(" "c)(0)
    strOldFirstName.Text = tmpStr

    tmpStr = lines(7).Substring(strOldFirstName.Text.Length).Trim
    strOldLastName.Text = tmpStr

    tmpStr = lines(14).Split(" "c)(0)
    strNewFirstName.Text = tmpStr
    tmpStr = lines(14).Substring(strNewFirstName.Text.Length).Trim
    strNewLastName.Text = tmpStr
End Sub

Example Output