我正在使用Visual Basic 2010制作一个模拟停车的程序。 当新车到达时,我将文本写入txt文件,格式如下:
#LicensePlate
全名
ArrivalDate
ArrivalTime
DepartDate
DepartTime
#继续使用上述格式。
如何通过输入他们的车牌让这个人找到该部分,然后它会显示该部分以便他们输入他们的离境详情?
谢谢!
答案 0 :(得分:0)
不要在普通的TXT文件中写入数据。而是使用XML文件。您可以使用XML解析器来更快地获取。
答案 1 :(得分:0)
这是一个答案。我用console作为样本。请参阅以下代码。
Imports System.Text
Imports System.IO
Module Module1
Sub Main()
Dim blnFound As Boolean = False
Console.WriteLine("LicensePlate: ")
Dim strLicensePlate As String = Console.ReadLine()
Dim text As String = File.ReadAllText("TextData.txt")
Dim index As Integer = text.IndexOf("" & strLicensePlate)
If index >= 0 Then
blnFound = True
End If
If blnFound = False Then
Console.WriteLine("FullName: ")
Dim strFullName As String = Console.ReadLine()
Console.WriteLine("ArrivalDate: ")
Dim strArrivalDate As String = Console.ReadLine()
Console.WriteLine("ArrivalTime: ")
Dim strArrivalTime As String = Console.ReadLine()
Using w As StreamWriter = File.AppendText("TextData.txt")
WriteData(strLicensePlate, strFullName, strArrivalDate, strArrivalTime, w)
End Using
Else
Console.WriteLine("DepartureDate: ")
Dim strDepartureDate As String = Console.ReadLine()
Console.WriteLine("DepartureTime: ")
Dim strDepartureTime As String = Console.ReadLine()
Dim lines() As String = File.ReadAllLines("TextData.txt")
For i As Integer = 0 To lines.Length - 1
If lines(i).Contains(strLicensePlate) Then
lines(i + 4) = "DepartureDate:" & strDepartureDate
lines(i + 5) = "DepartureTime:" & strDepartureTime
File.WriteAllLines("TextData.txt", lines)
Exit For
End If
Next
Display(strLicensePlate)
End If
Console.ReadKey()
End Sub
Private Sub WriteData(ByVal strLicensePlate As String, ByVal strFullName As String, ByVal strArrivalDate As String, _
ByVal strArrivalTime As String, ByVal w As TextWriter)
w.WriteLine("#")
w.WriteLine("LicensePlate: {0}", strLicensePlate)
w.WriteLine("FullName: {0}", strFullName)
w.WriteLine("ArrivalDate: {0}", strArrivalDate)
w.WriteLine("ArrivalTime: {0}", strArrivalTime)
w.WriteLine("DepartureDate:")
w.WriteLine("DepartureTime:")
End Sub
End Module
WriteData
方法用于在汽车到达时写入数据。如您所见,DepartureDate
和DepartureTime
为空。获取text.IndexOf
是要知道输入的LicensePlate
是否已存在。如果它尚不存在,则索引返回-1
。如果存在,则询问用户出发日期和时间。空白的出发日期和时间将替换为新输入的日期和时间。
File.ReadAllLines()
逐行获取txt数据,并逐行搜索lines(indexNumber).Contains
使用的关键字。如果lines(indexNumber).Contains
返回true,则使用此代码开始用新输入的数据替换空的出发日期和时间,
lines(i + 4) = "DepartureDate:" & strDepartureDate
lines(i + 5) = "DepartureTime:" & strDepartureTime
还有4和5的索引号,因为索引当前指向LicensePlate
的位置,而DepartureDate
和DepartureTime
是4
和{分别位于5
下方的{1}}行。
其他:
LicensePlate
上面的代码显示数据。只需将Private Sub Display(ByVal strLicensePlate As String)
Dim lines() As String = File.ReadAllLines("TextData.txt")
For i As Integer = 0 To lines.Length - 1
If lines(i).Contains(strLicensePlate) Then
Console.WriteLine(lines(i + 1))
Console.WriteLine(lines(i + 2))
Console.WriteLine(lines(i + 3))
Console.WriteLine(lines(i + 4))
Console.WriteLine(lines(i + 5))
File.WriteAllLines("TextData.txt", lines)
Exit For
End If
Next
End Sub
替换为您要将数据放置为Console.WriteLine
或textbox
的任何变量。并且不要忘记在label
Display(strLicensePlate)
部分内添加else