我正在处理我的项目,我有两个问题,我无法解决,试图在谷歌上找到并试图自己解决但没有运气
问题#1
当我尝试从richtextbox(多行)保存文本并将其替换为现有文本文件时,它不会替换文件,它只是将新文本添加到旧文本中。(对不起英语,我会举例说明,所以你可以得到它)
文本框文字:
NEWinformation1
NEWinformation2
Newinformation3
NEWinformation4
NEWinformation5
已存在的旧txt文件:
OLDinformation1
OLDinformation2
OLDinformation3
OLDinformation4
OLDinformation5
尝试替换现有文本文件时得到的结果:
NEWinformation1
NEWinformation2
Newinformation3
NEWinformation4
NEWinformation5
OLDinformation1
OLDinformation2
OLDinformation3
OLDinformation4
OLDinformation5
这是我的代码:
Dim Save As New SaveFileDialog()
Dim myStreamWriter As System.IO.StreamWriter
Save.Filter = "Text *.txt|*.txt"
Save.CheckPathExists = True
Save.Title = "Save File"
Save.ShowDialog(Me)
Try
myStreamWriter = System.IO.File.AppendText(Save.FileName)
myStreamWriter.Write(RTBOutput.Text)
myStreamWriter.Flush()
Catch ex As Exception
End Try
问题#2
我从richserverbox获取的文本是从我的服务器上删除的(www.example.com/information.txt) 当我从服务器保存多行文本时会出现问题,常规记事本会在一行中读取所有文本,但是当使用notepad ++并启用"显示所有字符串时#34;在np ++设置中,它分别读取evry行,但使用 LF 换行符代码而不是 CR LF
这是图片:
此代码用于从服务器抓取文本:
Dim reader As StreamReader = New StreamReader(client.OpenRead(SERVERADDRESS))
RTBOutput.Text = reader.ReadToEnd
LabelLineCount.Text = RTBOutput.Lines.Count
答案 0 :(得分:0)
回答#1
Dim Save As New SaveFileDialog()
Dim myStreamWriter As System.IO.StreamWriter
Save.Filter = "Text *.txt|*.txt"
Save.CheckPathExists = True
Save.Title = "Save File"
Save.ShowDialog(Me)
Try
myStreamWriter = System.IO.File.AppendText(Save.FileName) ''<-- Here is my fault
myStreamWriter.Write(RTBOutput.Text)
myStreamWriter.Flush()
Catch ex As Exception
End Try
用CreateText替换AppendText:
myStreamWriter = System.IO.File.CreateText(Save.FileName)
回答#2
使用下一个代码解决了这个问题:
Dim Save As New SaveFileDialog()
Save.Filter = "Text *.txt|*.txt"
Save.CheckPathExists = True
Save.Title = "Save File"
Save.ShowDialog(Me)
Using sw As New StreamWriter(Save.FileName)
For Each line As String In RTBoxShop.Lines
sw.WriteLine(line)
Next
End Using