我想在引号中显示文本并显示它。
名称“john”
并将其替换为文本框中的内容
名称“随便”
这都在.txt文件中
答案 0 :(得分:1)
您必须逐行阅读文件,逐字拆分并找到双引号文本,将其替换为文本框文本。
我已经给出了读取和替换的代码。在末尾创建一个写函数,将其写回txt文件。
用法:
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String
Dim FullContent as string=""
' Read and display the lines from the file until the end
' of the file is reached.
Dim strArray() as string=Nothing
Do
Array.Clear(strArray,0,strArray.Length)
line = sr.ReadLine()
strArray=line.split(" ") 'To read Every Word Seperated by a Space
For i=0 to strArray.Count-1
'Checks StartsWith and EndsWith " - Eg. Ashi likes "firefox"
If strArray(i).StartsWith("""") and strArray(i).EndsWith("""") then
'Replace the string in "firefox" with TextBox1 text
line.Replace("""" & strArray(i) & """",trim(TextBox1.Text))
End If
FullContent = FullContent & line 'Append the changes to full content
Next
Loop Until line Is Nothing
sr.Close()
'Now you can write the FullContent Back to Txt File which has the replaced changes
'writeFunction(FullContent)
'If you want to display, then
msgbox(FullContent)
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try
答案 1 :(得分:0)
尝试谷歌搜索vb.net + streamreader和vb.net + streamwriter示例,这应该会给你想要的。