vb.net - 从记事本中读取特定数据
问题描述
我有一个带有一些未加密数据的记事本,如下所示,我想从该文本文件中读取数据并将数据填充到各个文本框。
这就是我的输入的样子:
========================================================
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
根据珍贵的贝汀得到过载异常错误
更新 :
解决方案
如果您提供的示例文本文件始终如此,那么这应该可以。
我对大多数值进行了硬编码,但对于上面的文本文件,下面的代码有效:
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
推荐阅读
- node.js - Express / handlebars - 将数据从端点传输到端点
- tensorflow - 为什么 tf.GradientTape.jacobian 没有给出?
- c++ - 为什么在 OpenGL 中使用纹理会破坏我的其他对象?
- go - bash调用Go程序输入时的无限循环
- javascript - 在 javascript 中将文件保存到 Amazon S3 存储桶
- java - TensorFlowException:不成功的 TensorSliceReader 构造函数:未能找到 /mnt/yarn/usercache 的任何匹配文件
- d3.js - 随时间更新 D3 力模拟节点位置,无脉动效应
- sql - 为什么这不起作用?ORA-00979: 不是 GROUP BY 表达式
- javascript - Run Promise all in an await for loop
- php - Cloudflare HTTP_CF_CONNECTING_IP 未显示真实 IP