Results 1 to 11 of 11

Thread: How to delete the lines had been read in the txt file?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    258

    How to delete the lines had been read in the txt file?

    I want to use .net to read the information in a txt file, but after read it, I hope to delete the information that I had read, for example:
    in the txt file it has two line:

    111
    222

    after read these two lines, I hope to delete these two lines, so how to do it?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,684

    Re: How to delete the lines had been read in the txt file?

    It depends on how you're reading the file. Here is one way:
    Code:
    Private Function ReadThenDeleteLine(ByVal path As String) As String
        If Not IO.File.Exists(path) Then
            Return String.Empty
        Else
            Dim lines() As String = IO.File.ReadAllLines(path)
            If lines.Length > 0 Then
                IO.File.WriteAllLines(path, lines.Skip(1).ToArray())
                Return lines(0)
            Else
                Return String.Empty
            End If
        End If        
    End Function

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,121

    Re: How to delete the lines had been read in the txt file?

    Bottom line, as dday9 shows, you can't delete lines from the front of a file without reading the whole file in, then re-writing the whole file, but leaving out the part you want "deleted".

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    258

    Re: How to delete the lines had been read in the txt file?

    Quote Originally Posted by passel View Post
    Bottom line, as dday9 shows, you can't delete lines from the front of a file without reading the whole file in, then re-writing the whole file, but leaving out the part you want "deleted".
    Or can show how to delete the whole lines in the file?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    258

    Re: How to delete the lines had been read in the txt file?

    Quote Originally Posted by dday9 View Post
    It depends on how you're reading the file. Here is one way:
    Code:
    Private Function ReadThenDeleteLine(ByVal path As String) As String
        If Not IO.File.Exists(path) Then
            Return String.Empty
        Else
            Dim lines() As String = IO.File.ReadAllLines(path)
            If lines.Length > 0 Then
                IO.File.WriteAllLines(path, lines.Skip(1).ToArray())
                Return lines(0)
            Else
                Return String.Empty
            End If
        End If        
    End Function
    THX, Or can show how to delete the whole lines in the file?

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,684

    Re: How to delete the lines had been read in the txt file?

    I'm sorry, but I have no idea what you're asking.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    258

    Re: How to delete the lines had been read in the txt file?

    Quote Originally Posted by dday9 View Post
    I'm sorry, but I have no idea what you're asking.
    I mean, delete whole information in the txt file which the program is reading...

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,684

    Re: How to delete the lines had been read in the txt file?

    Do you mean that you want to remove all text from the file? If so then write to the file an empty string:
    Code:
    IO.File.WriteAllText("my_path_here.txt", String.Empty)

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    258

    Re: How to delete the lines had been read in the txt file?

    Quote Originally Posted by dday9 View Post
    Do you mean that you want to remove all text from the file? If so then write to the file an empty string:
    Code:
    IO.File.WriteAllText("my_path_here.txt", String.Empty)
    OK, thanks

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,286

    Re: How to delete the lines had been read in the txt file?

    Or just delete the file

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    258

    Re: How to delete the lines had been read in the txt file?

    Quote Originally Posted by DataMiser View Post
    Or just delete the file
    OK, thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width