Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • *Experts*
Posted

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.

Dim writer As New IO.StreamWriter("file path", False)

Posted (edited)

rrrr....it still doesn't overwrite the file..I don't 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?

Edited by leontager
  • *Experts*
Posted

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:

IO.File.Delete("path")

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...