将整个文件读取到一个数组中,替换该数组中的一个元素,然后再次写入整个文件.文本的结尾是一样的



我有一个form3,当用户退出时,它会试图节省他们玩游戏的时间。当我试图用完全相同的文本替换这一行时,文件中没有任何变化。为什么不替换文本?提前谢谢。

File.AppendAllLines(Application.StartupPath + "contentmainuserdatatgametime.txt", FileLines)
'this line will not write to file if it has the same file name because FileLines is still using it, if this could be solved too awesome!
Private Sub SaveGameTime()
Dim fullpath = Path.Combine(Application.StartupPath + "contentmainuserdatagametime.txt")
Dim FileLines = File.ReadLines(fullpath)
Dim alltext = File.ReadAllText(fullpath)
Dim datalist = New List(Of String)
Dim line
datalist.Add(tbGameName.Text + " : " + LBtimeout.Text)
TextDiag(FileLines.Count.ToString, "Clear")
If FileLines.Count > 0 Then
If alltext.Contains(tbGameName.Text) Then
For Each line In FileLines
TextDiag(line, "Clear")

line.Replace(line, tbGameName.Text + " : " + LBtimeout.Text)
TextDiag("new line: " + line, "Clear")

Next
File.AppendAllLines(Application.StartupPath + "contentmainuserdatatgametime.txt", FileLines)


Else
TextDiag("Line not found, adding new line", "Clear")
File.AppendAllLines(fullpath, datalist)
End If
Else
TextDiag("No lines not found, adding new line", "Clear")
File.AppendAllLines(fullpath, datalist)
End If
datalist.Clear()
fullpath = Nothing
FileLines = Nothing
datalist = Nothing

TextDiag输出

让它工作起来,在格式如下的文件中替换指定字符串值的文本:"文本>0:0:0"。它得到一个新的时间值,该值是从一个格式如下的标签中提取的:;0:0:0";,删除除数字和冒号以外的所有内容:;0:0:0";,将它们格式化为TimeSpan,并将它们添加到还包含任何其他原始文本的输出行中。

  • 谢谢
Dim fullpath = "yourpath"
Dim FileLines = File.ReadLines(fullpath)
Dim outputlines As New List(Of String)
Dim alltext = File.ReadAllText(fullpath)
Dim datalist = New List(Of String)
Dim line
Dim lbtime = TimeSpan.Parse(LBtimeout.Text.Replace(" ", ""))

datalist.Add("youstring" + " : " + "yourstring")

If FileLines.Count > 0 Then
If alltext.Contains("yourstring") Then
For Each line In FileLines
If line.contains("yourstring") Then

Dim liness = line.substring(line.indexof(">") + 1)
Dim linerep = liness.replace(" ", "")

Dim linetime = TimeSpan.Parse(linerep)

Dim timesum As TimeSpan = lbtime + linetime
line.replace(line, "")
line = "yourstring" + " > " + timesum.ToString

outputlines.Add(line)
Else
outputlines.Add(line)
End If
Next

File.WriteAllLines("yourpath", outputlines)

Else

File.AppendAllLines(fullpath, datalist)
End If
Else
'No lines not found, adding new line
File.AppendAllLines(fullpath, datalist)
End If
datalist.Clear()
fullpath = Nothing
FileLines = Nothing
datalist = Nothing

相关内容

  • 没有找到相关文章