从记事本中读取特定数据
Read specific data from 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\Desktop3.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
根据 preciousbetine 获取过载异常错误
已更新:
如果您提供的示例文本文件始终如一,那么这应该可行。
我对大部分值进行了硬编码,但对于上面的文本文件,下面的代码有效:
Sub GetInfo()
Dim path As String = "C:\Users\XXX\Desktop3.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
我有一个记事本,其中包含一些未加密的数据,如下所示,我想从该文本文件中读取数据并将数据填充到各个文本框中。
我的输入是这样的:
========================================================
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\Desktop3.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
根据 preciousbetine 获取过载异常错误
已更新:
如果您提供的示例文本文件始终如一,那么这应该可行。
我对大部分值进行了硬编码,但对于上面的文本文件,下面的代码有效:
Sub GetInfo()
Dim path As String = "C:\Users\XXX\Desktop3.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