2012-04-12 91 views

回答

7
Dim fileReader As String = My.Computer.FileSystem.ReadAllText("C:\test.txt").Replace("foo", "bar") 
My.Computer.FileSystem.WriteAllText("C:\test2.txt", fileReader, False) 

所以,你應該使用的文件系統的ReadAllText方法,傳遞路徑作爲參數,並用替換法更換要更換什麼。對於更高級的替換用法,可以使用正則表達式。你可以閱讀關於here

較短的版本:

My.Computer.FileSystem.WriteAllText("C:\test2.txt", My.Computer.FileSystem.ReadAllText("C:\test.txt").Replace("foo", "bar"), False) 
+0

Thaks生病試試這個 – 2012-04-12 19:16:09

+0

謝謝我複製粘貼您的代碼在命令按鈕單擊事件,當我建立程序時,當我點擊按鈕沒有替換完成的文本文件,它仍然填充與富。 – 2012-04-12 19:25:49

+0

我明白了。 String fileReader包含正確的文本嗎?你的問題是該文件不刷新。我的代碼僅用於閱讀文本,寫作還沒有完成,但我很快就會更新我的代碼。 – 2012-04-12 19:31:44

0

試試下面的例子中,我希望它可以幫助

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 
+0

由於生病嘗試這個 – 2012-04-12 18:54:25

+0

錯誤 '打開' 未聲明。文件I/O功能可在'Microsoft.VisualBasic'命名空間中找到。 \t C:\ Users \ EMACFEL \ AppData \ Local \ Temporary Projects \ WindowsApplication1 \ Form1.vb WindowsApplication1 – 2012-04-12 19:02:33

+0

我做錯了什麼? – 2012-04-12 19:03:00

1

我把這個了一個程序,我做的,只是切出你不需要的垃圾。

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 
1

我有一個小程序,我用它來重複使用代碼。當我需要創建一個類或代碼文件時,我使用它,它與現有的類幾乎相同。它是一個基本的表單程序,包含四個文本框和一個按鈕,它們使用與接受的答案相同的命令,但字符串不是硬編碼的。

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 
相關問題