Vb.net 使用Visual Basic查找和替换txt文档中的文本
您好,我使用的是Visual Basic 2008 Express Edition,我的目标是读取作为模板的整个.txt文件,并在您按下命令按钮时将单词的所有文档替换为新文档,并将新修改的文本保存为新的.txt。有人能给我一些建议吗?试试下面的例子,希望能有所帮助Vb.net 使用Visual Basic查找和替换txt文档中的文本,vb.net,Vb.net,您好,我使用的是Visual Basic 2008 Express Edition,我的目标是读取作为模板的整个.txt文件,并在您按下命令按钮时将单词的所有文档替换为新文档,并将新修改的文本保存为新的.txt。有人能给我一些建议吗?试试下面的例子,希望能有所帮助 Dim lOpenFile As Long Dim sFileText As String Dim sFileName As String sFileName = "C:\test.txt" 'open the file
Dim lOpenFile As Long
Dim sFileText As String
Dim sFileName As String
sFileName = "C:\test.txt"
'open the file and read it into a variable
lOpenFile = FreeFile
Open sFileName For Input As lOpenFile
sFileText = Input(LOF(lOpenFile), lOpenFile)
Close lOpenFile
'change 'John Doe' to 'Mary Brown'
sFileText = Replace(sFileText, " John Doe ", " Mary Brown ")
'write it back to the file
lOpenFile = FreeFile
Open sFileName For Output As lOpenFile
Print #lOpenFile, sFileText
Close lOpenFile
因此,您应该使用文件系统的ReadAllText方法,将路径作为参数传递,并使用Replace方法替换要替换的内容。有关replace的更高级用法,可以使用正则表达式。你可以读一下
较短版本:
My.Computer.FileSystem.WriteAllText("C:\test2.txt", My.Computer.FileSystem.ReadAllText("C:\test.txt").Replace("foo", "bar"), False)
我是从我正在制作的一个程序中提取出来的,只是去掉你不需要的废话
Private Sub UPDATECODES_Click(sender As Object, e As EventArgs) Handles UPDATECODES.Click
Dim NEWCODE As String = NEWCODEBOX.Text
Dim CC As String = CURRENTCODE.Text
Dim oldcode As String
Dim codelines(0 To 1) As String
Dim olddate As String
Using r1 As New System.IO.StreamReader(CODEDIR & "d.dat")
olddate = r1.ReadToEnd
End Using
Using r2 As New System.IO.StreamReader(CODEDIR & "oc.dat")
oldcode = r2.ReadToEnd
End Using
If System.IO.File.Exists(CODEDIR & "new code.txt") Then
System.IO.File.Delete(CODEDIR & "new code.txt")
End If
If System.IO.File.Exists(CODEDIR & "CC.DAT") Then
If IO.File.Exists(CODEDIR & "oc.dat") Then
IO.File.Delete(CODEDIR & "OC.DAT")
End If
My.Computer.FileSystem.RenameFile(CODEDIR & "CC.DAT", "OC.DAT")
Dim FILESTREAM As System.IO.FileStream
FILESTREAM = New System.IO.FileStream(CODEDIR & "CC.DAT", System.IO.FileMode.Create)
FILESTREAM.Close()
End If
Using WRITER As New System.IO.StreamWriter(CODEDIR & "CC.DAT")
WRITER.WriteLine(NEWCODE)
End Using
Dim currentlines(0 To 1) As String
Dim a As Integer
Dim TextLine(0 To 1) As String
a = 0
Using sr As New System.IO.StreamReader(CODEDIR & "internet code.txt")
While Not sr.EndOfStream
ReDim codelines(0 To a)
codelines(a) = sr.ReadLine()
codelines(a) = codelines(a).Replace(CURRENTCODE.Text, NEWCODE)
codelines(a) = codelines(a).Replace(olddate, DATEBOX1.Text & " - " & DATEBOX2.Text)
Dim newfile As String = (CODEDIR & "new code.txt")
Using sw As New System.IO.StreamWriter(newfile, True)
sw.WriteLine(codelines(a))
End Using
a = a + 1
End While
End Using
Using sw2 As New System.IO.StreamWriter(CODEDIR & "d.dat")
sw2.WriteLine(date1 & " - " & date2)
End Using
System.IO.File.Delete(CODEDIR & "internet code.txt")
My.Computer.FileSystem.RenameFile(CODEDIR & "new code.txt", "internet code.txt")
CURRENTCODE.Text = NEWCODE
End Sub
我有一个我写的小程序,我用它来重用代码。当我需要创建一个与现有类或代码文件几乎相同的类或代码文件时,我会使用它。它是一个基本表单程序,有四个文本框和一个按钮,除了字符串没有硬编码外,它使用与接受答案相同的命令
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim source As String = txtSource.Text
Dim destination As String = txtDestination.Text
Dim oldText As String = txtOldText.Text
Dim newText As String = txtNewText.Text
My.Computer.FileSystem.WriteAllText(destination, My.Computer.FileSystem.ReadAllText(source).Replace(oldText, newText), False)
End Sub
未声明错误1“打开”。文件I/O功能在“Microsoft.VisualBasic”命名空间中可用。C:\Users\EMACFEL\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 32 9 WindowsApplication1使用System.IoTS添加系统IO我复制粘贴了您的代码在命令按钮单击事件中,当我构建程序时,当我点击按钮时,文本文件中仍然充满了foo,但没有替换完成。我明白了。字符串文件读取器包含正确的文本,对吗?您的问题是文件没有刷新。我的代码只用于阅读文本,尚未完成编写,但我很快会更新我的代码。好的,我添加了
My.Computer.FileSystem.writealText(“C:\Users\EMACFEL\Documents\test.txt”,fileReader,False)
,它成功了thanks@DNA180,这可能可以用正则表达式实现。没关系,我在这里找到了。这是做什么的?“只需删掉你不需要的废话”——你没有解释代码的作用,删掉代码不是更容易吗?
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim source As String = txtSource.Text
Dim destination As String = txtDestination.Text
Dim oldText As String = txtOldText.Text
Dim newText As String = txtNewText.Text
My.Computer.FileSystem.WriteAllText(destination, My.Computer.FileSystem.ReadAllText(source).Replace(oldText, newText), False)
End Sub