2011-06-23 121 views
0

我目前正在尋找一種刪除txt文件中的第(n)行和第(n)行的方法。例如每2日和5日行。 有沒有用腳本或C#做到這一點的方法?從文本文件中刪除每個第二行和第三行

+0

可能重複http://stackoverflow.com/questions/4264117/c-how-to-delete-last-line-in-a -文本文件) – Treffynnon

回答

0

此代碼是在VB.NET中,但我相信這會做你想做的?

Dim sr As streamreader = Nothing 
    Dim sw As StreamWriter = Nothing 
    Dim LineString As String = "" 
    Dim LineNum As Integer = 0 
    Try 
     sr = New StreamReader("C:\scratch\input.txt") 
     sw = New StreamWriter("c:\scratch\output.txt") 
     Do Until sr.EndOfStream 
      LineString = sr.ReadLine 
      LineNum += 1 

      If LineNum Mod 2 = 0 Then 
       'don't output 2nd line 
      ElseIf LineNum Mod 5 = 0 Then 
       'don't output 5th line 
      Else 
       'write it 
       sw.WriteLine(LineString) 
      End If 
     Loop 
    Catch ex As Exception 
     MsgBox("Error - " & ex.Message) 
    Finally 
     If Not IsNothing(sr) Then sr.Close() 
     If Not IsNothing(sw) Then sw.Close() 
    End Try 
的[C#如何刪除最後一行在文本文件?(
相關問題