Vb.net 如何替换一行中的多个连续行并跳过该节的标题
我需要在用于保存程序设置的文本文件中找到一个节头,在本例中为“[Store Hours]”。我需要跳过标题,用文本框中的文本替换接下来的7行。下面的代码当前删除标题“[Store Hours]”,并且不替换任何行Vb.net 如何替换一行中的多个连续行并跳过该节的标题,vb.net,Vb.net,我需要在用于保存程序设置的文本文件中找到一个节头,在本例中为“[Store Hours]”。我需要跳过标题,用文本框中的文本替换接下来的7行。下面的代码当前删除标题“[Store Hours]”,并且不替换任何行 Dim objFileName As String = "Settings.txt" Private Sub BtnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveHours.
Dim objFileName As String = "Settings.txt"
Private Sub BtnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveHours.Click
Dim OutPutLine As New List(Of String)()
Dim matchFound As Boolean
For Each line As String In System.IO.File.ReadAllLines(objFileName)
matchFound = line.Contains("[Store Hours]")
If matchFound Then
'does not skip the header line
line.Skip(line.Length)
'Need to loop through this 7 times (for each day of the week)
'without reading the header again
For intCount = 0 To 6
Dim aryLabelDay() As String = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
Dim varLabelNameIn As String = "txt" & aryLabelDay(intCount).ToString & "In"
Dim varDropNameIn As String = "drp" & aryLabelDay(intCount).ToString & "In"
Dim varLabelNameOut As String = "txt" & aryLabelDay(intCount).ToString & "Out"
Dim varDropNameOut As String = "drp" & aryLabelDay(intCount).ToString & "Out"
Dim varTextBoxInControl() As Control = Me.Controls.Find(varLabelNameIn, True)
Dim varDropBoxInControl() As Control = Me.Controls.Find(varDropNameIn, True)
Dim varTextBoxOutControl() As Control = Me.Controls.Find(varLabelNameOut, True)
Dim varDropBoxOutControl() As Control = Me.Controls.Find(varDropNameOut, True)
Dim dymTextNameIn As TextBox = DirectCast(varTextBoxInControl(0), TextBox)
Dim dymDropNameIn As ComboBox = DirectCast(varDropBoxInControl(0), ComboBox)
Dim dymTextNameOut As TextBox = DirectCast(varTextBoxOutControl(0), TextBox)
Dim dymDropNameOut As ComboBox = DirectCast(varDropBoxOutControl(0), ComboBox)
Dim ReplaceLine As String
ReplaceLine = dymTextNameIn.Text & "," & dymDropNameIn.Text & "," & dymTextNameOut.Text & "," & dymDropNameOut.Text
'this doesn't replace anything
line.Replace(line, ReplaceLine)
intCount += 1
Next intCount
Else
OutPutLine.Add(line)
End If
Next
End Sub
不使用ReadAllLines,只需使用streamreader并逐行读取文件,如下所示
Dim line As String
Using reader As StreamReader = New StreamReader("file.txt")
' Read one line from file
line = reader.ReadLine
If(line.Contains("[Store Hours]") 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
但更重要的是,您不应该将程序的设置保存在文本文件中。它们应该存储在app.config或web.config中。有关这方面的进一步指导,请参阅 您的困惑可能部分来自这样一个事实,即您不能只替换文本文件的一部分而不复制和覆盖它。一种方法是将文件复制到内存中,更改相应的行并用新信息覆盖现有文件。这里有一种方法可以做到:
Private Sub BtnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveHours.Click
Dim OutPutLine As New List(Of String)()
Dim sr As New StreamReader(objFileName)
While Not sr.EndOfStream
Dim line = sr.ReadLine
'Found the header so let's save that line to memory and add all the other _
info after it.
If line.Contains("[Store Hours]") Then
OutPutLine.Add(line)
'Need to loop through this 7 times (for each day of the week)
'without reading the header again
Dim aryLabelDay() As String = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
For intCount = 0 To 6
'even though we're not using the info from the file, calling _
Readline advances the file pointer so that the next iteration _
of the while loop we can finish reading the file.
If Not sr.EndOfStream Then
line = sr.ReadLine
Dim dymTextNameIn As TextBox = DirectCast(Me.Controls("txt" & aryLabelDay(intCount) & "In"), TextBox)
Dim dymDropNameIn As ComboBox = DirectCast(Me.Controls("drp" & aryLabelDay(intCount) & "In"), ComboBox)
Dim dymTextNameOut As TextBox = DirectCast(Me.Controls("txt" & aryLabelDay(intCount) & "Out"), TextBox)
Dim dymDropNameOut As ComboBox = DirectCast(Me.Controls("drp" & aryLabelDay(intCount) & "Out"), ComboBox)
OutPutLine.Add(dymTextNameIn.Text & "," & dymDropNameIn.Text & "," & dymTextNameOut.Text & "," & dymDropNameOut.Text)
End If
Next
Else
'Any line that isn't in that section gets copied as is.
OutPutLine.Add(line)
End If
End While
'Copy all the new info to the same file overwriting the old info.
File.WriteAllLines(objFileName, OutPutLine)
End Sub
旁注。控件集合按编号或名称编制索引,这使得只需知道其名称即可访问相应控件变得相当简单。感谢您的帮助,我终于找到了它,这是我使用的最终代码
Private Sub btnSaveHours_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveHours.Click
Dim intOutPutLine As New List(Of String)()
Dim blnSearchString As Boolean
Dim intLineCount As Integer = -1
Dim intLoopCount As Integer
For Each line As String In System.IO.File.ReadAllLines(objFileName)
blnSearchString = line.Contains("[Store Hours]")
If blnSearchString Then
intLineCount = intOutPutLine.Count
line.Remove(0)
intLoopCount = 0
ElseIf intLineCount = intOutPutLine.Count And intLoopCount < 7 Then
line.Length.ToString()
line.Remove(0)
intLoopCount += 1
Else
intOutPutLine.Add(line)
End If
Next
System.IO.File.WriteAllLines(objFileName, intOutPutLine.ToArray())
Dim objFileWrite As New StreamWriter(objFileName, True)
If File.Exists(objFileName) Then
objFileWrite.WriteLine("[Store Hours]")
Dim varMe As Control = Me
Call subConvertFrom12to24Hours(objFileWrite, varMe)
Else
objFileWrite.WriteLine("[Store Hours]")
Dim varMe As Control = Me
Call subConvertFrom12to24Hours(objFileWrite, varMe)
End If
Call btnClear_Click(sender, e)
End Sub
Private Sub BTNSavehurs\u Click(发送方作为System.Object,e作为System.EventArgs)处理BTNSavehurs。单击
Dim intOutPutLine作为新列表(字符串)()
作为布尔值的Dim blnSearchString
Dim INTLINECUNT为整数=-1
Dim intLoopCount为整数
对于System.IO.File.ReadAllLines(objFileName)中的字符串形式的每一行
blnSearchString=line.Contains(“[Store Hours]”)
如果是blnSearchString,则
intLineCount=intOutPutLine.Count
行。删除(0)
intLoopCount=0
ElseIf intLineCount=intOutPutLine.Count和intLoopCount<7然后
line.Length.ToString()
行。删除(0)
intLoopCount+=1
其他的
输入输出行。添加(行)
如果结束
下一个
System.IO.File.writeAllines(objFileName,intOutPutLine.ToArray())
Dim objFileWrite作为新StreamWriter(objFileName,True)
如果File.Exists(objFileName)存在,则
objFileWrite.WriteLine(“[存储时间]”)
Dim varMe As Control=Me
在12到24小时内呼叫Subconvert(objFileWrite,varMe)
其他的
objFileWrite.WriteLine(“[存储时间]”)
Dim varMe As Control=Me
在12到24小时内呼叫Subconvert(objFileWrite,varMe)
如果结束
呼叫btnClear\u单击(发件人,e)
端接头
line.Skip(line.Length)line根本不起作用。我不确定您认为它在做什么,但我向您保证,它不是。App/web.config需要更多的开销来读取,并且不是存储业务数据的好地方。它实际上应该只用于与应用程序设置相关的应用程序数据。Web.config文件应该加密,因此很难更改它们。