simple io problem

leontager

Well-known member
Joined
Jun 17, 2003
Messages
89
hi, I am trying to write a file in a loop. Each time it should write seomthing differnet. Instead of rewriting the file, my program adds the new information ot hte file. how can I make it rewrite.

my code:

Dim writer As New IO.StreamWriter("C:\my folder\ad.html")
For i = 1 To 5
........
reget(uName)
jLinks = String.Join(",", v_link, 0, v_rem)
writer.Write(jLinks)
writer.Flush()
next


now jLinks changes each time but it is being added at the end of the file while I need it to replace the whole file. I thought about erasing it so that It would create a new one each time but I get a sharing violation. I am pretty sure this is simple to do, just not sure how:p
 
Another overload of the StreamWriter constructor asks whether you wish to append to the file, just specify it to False. It will overwrite the file.
Code:
Dim writer As New IO.StreamWriter("file path", False)
 
rrrr....it still doesnt overwrite the file..I dont understand.

Dim writer As New IO.StreamWriter("C:\my folder\ad.html", False)
Dim stream As IO.StreamReader
Dim wc As New Net.WebClient()

Dim i As Integer
Dim m As Integer
stream = New IO.StreamReader(wc.OpenRead("C:\my folder\file.htm"))

For i = 1 To 5
reget(uName)
jLinks = String.Join(",", v_link, 0, v_rem)
writer.Write( jLinks _
& "];" & stream.ReadToEnd)
writer.Flush()
pr(1).Start("IEXPLORE", "C:\my folder\ad.html")
Next



is there anyway I could delete the file each time?
 
Last edited by a moderator:
It should work, it should empty the file right during the constructor. Are you sure you are checking the right file or things like that?
To delete a file use the IO.File class:
Code:
IO.File.Delete("path")
 
Back
Top