首页 > 解决方案 > 从记事本中读取特定数据

问题描述

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

这就是我的输入的样子:

========================================================
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

根据珍贵的贝汀得到过载异常错误

在此处输入图像描述

更新 :

在此处输入图像描述

标签: vb.netnotepad

解决方案


如果您提供的示例文本文件始终如此,那么这应该可以。

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

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

示例输出


推荐阅读