Read text from file

Hi all

I have this code:
Private Sub readjFile()
        Dim pathAs String = "D:\Source\ex.txt"
        If File.Exists(path) Then
            Dim ulaznaLinija As String
            Dim fs As FileStream = New FileStream(putanja, FileMode.Open, FileAccess.Read)
            Dim sr As New StreamReader(fs)
            sr.BaseStream.Seek(0, SeekOrigin.Begin)
            Do While sr.Peek >= -1
                ulaznaLinija = sr.ReadLine
                If Not (ulaznaLinija Is Nothing) Then
                    proces(ulaznaLinija)
                End If
            Loop 'While Not (ulaznaLinija Is Nothing)
            fs.Flush()
            sr.Close()
        End If
    End Sub

when I read this "ulaznaLinija" I want to delete it from the file on  top ("D:\Source\ex.txt")
amaimediaAsked:
Who is Participating?
 
DhaestCommented:
Do you process all the lines and then at the end of your procedure, you'l need an empty file ?

Dim pathAs String = "D:\Source\ex.txt"

Dim oRead As System.IO.StreamReader
Dim LineIn As String
' Open your file
oRead = IO.File.OpenText(pathAs)
' Read it line by line
While oRead.Peek <> -1
   ' Read a line
   LineIn = oRead.ReadLine()

   Process (LineIn)

End While

oRead.Close()

         'In order to write back, we'll need to overwrite the file, so simply delete it here
         IO.File.Delete(pathAs )

         'Create a stream-writer for writing back to the file
         Dim sw As New System.IO.StreamWriter(pathAs )
         sw.Write("")

         'Dispose of the stream-writer - this will also flush the file to disk
         sw.Close()
0
 
DhaestCommented:
Do I understand it correctly ?
- You are reading a file
- In this file you want to search a specific string
- If you find this string, you want it to be removed from the file
0
 
amaimediaAuthor Commented:
I read the file line per line, and when I have read a line, I send it to: proces(ulaznaLinija). When tihis proces (ulaznaLinija) has finished I want to delete this line from the file.

 
0
 
DhaestCommented:
Why would you delete the line ? If you read the complete file line by line, you can process every line and afterwards just delete the complete file !

Dim pathAs String = "D:\Source\ex.txt"

Dim oRead As System.IO.StreamReader
Dim LineIn As String
' Open your file
oRead = IO.File.OpenText("C:\temp\subparts.txt")
' Read it line by line
While oRead.Peek <> -1
   ' Read a line
   LineIn = oRead.ReadLine()

   Process (LineIn)

End While

oRead.Close()

system.io.file.delete pathAs
0
 
amaimediaAuthor Commented:
I can't delete this file, becouse I will need it again. When I have read the whole file I actually want to clear all lines.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.