2012-04-12 21 views
5

kullanarak bir txt belgedeki metni bul ve değiştir Merhaba, Visual Basic 2008 Express Edition'ı kullanarak, şablon olarak çalışan ve bir sözcüğün tüm aktarımlarını yenisiyle değiştiren tüm bir .txt dosyasını okumak ve Bir komut düğmesine bastığınızda bu yeni değiştirilmiş metni yeni bir .txt dosyasına kaydedin. Birisi bana birkaç ipucu verebilir mi?Visual Basic

cevap

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

Yani FileSystem ait ReadAllText yöntemi kullanmalısınız yardımcı olur umarım şu örneği deneyin değiştirmek istediğiniz kitabın yerini alacak yöntemini değiştirin parametre olarak yolunu geçmek ve kullanmak . Değiştirmenin daha gelişmiş kullanımları için normal ifadeler kullanabilirsiniz. Bu konuda here okuyabilirsiniz.

Kısa versiyon:

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

Thaks has bunu deneyin –

+0

Teşekkürler Kopyala kodunuzu komut düğmesi tıklama olayına yapıştırdım ve programı oluşturduğumda, düğmeyi tıklattığımda hala değiştirilmemiş metin dosyasına yapılan bir değişiklik yok foo ile. –

+0

Anladım. String fileReader doğru metni doğru içeriyor mu? Sorununuz, dosyanın yenilenmemesidir. Kodum sadece metni okumak için yapıldı, henüz yazılmadı, ancak kodumu yakında güncelleyeceğim. –

0

Ben

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

sayesinde hasta bu –

+0

Hata 'Aç' bildirilmedi deneyin. Dosya I/O işlevselliği 'Microsoft.VisualBasic' ad alanında kullanılabilir. \t C: \ Kullanıcılar \ EMACFEL \ AppData \ Yerel \ Geçici Projeler \ WindowsApplication1 \ Form1.vb WindowsApplication1 –

+0

Yanlış bir şey yapıyorum? –

1

Sadece ihtiyacım yok bok kesip, ben yapıyordum bir bir programın dışında bu çekti.

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

Kodu yeniden kullanmak için kullandığımı yazdığım küçük bir programım var. Bir sınıf oluşturmam gerektiğinde ya da kod dosyasını, varolan biriyle hemen hemen aynı olduğunda kullanıyorum. Bu dört metin kutuları ile temel bir form programı ve dizeleri sabit kodlanmış dışında kabul edilen cevap olarak aynı komutu kullanan bir düğme.

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 
İlgili konular