Download Text File And Keep Strings?

Conejo

Well-known member
Joined
Jul 24, 2013
Messages
65
Location
USA
Programming Experience
1-3
Hi im trying to download a text file which has multiple lines but this happens:
Text file
hi line1
hi line2
But when i download it looks like this
hi line1hi line2

i tried using & vbcrlf but it didnt work here is my code:

On Error Resume Next
System.IO.File.Delete("viruslist.txt")
My.Computer.Network.DownloadFile(
"http://manticoreantivirus.altervista.org/viruslist.txt",
Application.StartupPath & "\viruslist.txt")
 
Hi,

I had a quick look at this and found the same issue as you with your own method and using the DownloadFile method of the WebClient class. After a little research I found that if a file is on a Linux or Unix Server then the file only has a LF character as the line separator as apposed to the usual CRLF characters and the solution was not to use Binary when downloading the file. I could not find the option to set this property using your solution or the DownloadFile method of the WebClient class and so I came up with a slightly different solution using the WebClient class in conjunction with a Stream.

Have a go with this:-

Using myClient As New WebClient, myStream As Stream = myClient.OpenRead("http://manticoreantivirus.altervista.org/viruslist.txt"),
      myReader As New StreamReader(myStream), myWriter As New StreamWriter("c:\temp\VirusData.txt")
 
  Do While Not myReader.EndOfStream
    myWriter.WriteLine(myReader.ReadLine)
  Loop
End Using
MsgBox("Done")


Hope that helps.

Cheers,

Ian
 
when i download it looks like this
hi line1hi line2
That would depend what editor you use when you "look". For example Notepad will only show as multiple lines if there is a CRLF line terminator, while for example Wordpad will show as multiple lines if either CR, LF or both are present. .Net methods for reading lines such as StreamReader.ReadLine and File.ReadAllLines will likewise recognize either. Some editors will only use one of those as line terminator when saving a file, and in .Net you can see similar with StreamWriter class where you can use NewLine property to specify which line terminator you want.

Note also that uploading or downloading a file does not change its contents, even if server is running a different OS, there is no "issue" with DownloadFile method in this regard.
On Error Resume Next
Look into using structured exception handling instead.
 
Thanks Ian for the code! But I realized that what said John was true my program still read the text file as if the lines were normal, Thanks! :eek:
 
Back
Top