У меня есть этот текстовый файл: с 4 соответствующими текстовыми полями: TxtStringNumP1, TxtStringNumP2, TxtStringNumP3 и TxtStringNumP4, мне нужно отобразить соответствующие значения: в текстовых полях: то есть, чтобы отобразить эти значения (числа) в соответствующем поле . вот как выглядит мой текстовый файл:
[TxtStringNumP1]
4
5
2
4
[TxtStringNumP2]
5
10
4
6
6
5
10
Как мне написать этот код, чтобы он работал?
Dim line As String
Using reader As StreamReader = New StreamReader(My.Application.Info.DirectoryPath + ("\Number.txt")
' Read one line from file
line = reader.ReadLine
If(line.Contains("[TxtStringNumP1]") Then
'The current line is the store hours header, so we skip it (read the next line)
line = reader.ReadLine
'Process the line like you want, and keep processing through the lines by doing a readline each time you want to progress to the next line.
End If
End Using
Я должен сделать так, чтобы он выглядел так:
TxtStringNumP1.Text
=
4
5
2
4
TxtStringNumP2.Text
=
5
10
4
6
Другой код:
Если вы хотите применить некоторые logi c построчно строка пропускает ненужные строки, затем считывает файл построчно или обрабатывает строки по одной после чтения их в памяти (выбор зависит от размера файла)
Этот подход использует расширение IEnumerable Где на возвращаемых строках by ReadLines (который возвращает IEnumerable ваших строк и не загружает их все в память).
For Each f In dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
Dim lines = File.ReadLines(f.FullName).
Where(Function(x) Not x.Contains("Kleur"))
' lines contains only the lines without the word Kleur
For Each l As String In lines
' Process the lines
Next
Next
Но вы также можете использовать StreamReader для чтения одной строки, обработать ее при необходимости, затем l oop для следующей строки
Dim line As String = ""
For Each f In dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
Using sr = New StreamReader(f.FullName)
While True
line = sr.ReadLine
If line IsNot Nothing Then
If Not line.Contains("Kleur") Then
' process the line
End If
Else
Exit While
End If
End While
End Using
Next
Наконец, вы можете загрузить все в память и обрабатывать оттуда (но обратите внимание на размер файла)
Dim line As String = ""
For Each f In dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
Dim lines = File.ReadAllLines(f.FullName)
For each line in lines
if Not line.Contains("Kleur") Then
' process the line
End If
Next
Next