2011-06-23 99 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#如何删除最后一行在文本文件?(
相关问题