Click here to Skip to main content
15,611,970 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hello, I am trying to read text from a text file located on an FTP server using VB. I have managed to figure out how to download the file and then read the data, but this is less than ideal. I would like to stream the text into a string where I can manipulate it and move on without ever having to download the file. I am moving very little data...maybe 30kb at most. Any help is appreciated. Thanks

Have you tried using a WebBrowser control and navigating to that file?



Dim txtStr as String = WebBrowser1.DocumentText

txtStr will have the source of the page you have navigated to - which ultimately is the content of test.txt
Share this answer
blackbanjo 11-Jun-10 1:39am    
I tried that, but i end up getting the page source, not just the text.

I also need to use a login and password, and this method provides no good way to rectify that.
Anshul R 11-Jun-10 1:45am    
For that, there will be a password box.

Use SendKeys.SendWait(Make sure your application is active that time)

First send the Username(as a string)
Then send "{TAB}" - For going to the password box
Then send the password(as a string)
Then send "{TAB}" - For focusing on the OK button.
Then send "{Return}" - equivalent of clicking OK button
blackbanjo 11-Jun-10 1:55am    
That may work, but it's not the approach I want to take. I was hoping I could use FTP and read the data into a string. There must be a way to do this.
Anshul R 11-Jun-10 1:56am    
To my knowledge, .NET Framework does not have this capability. You might have to write your own FTP Client.

There are good FTP projects in CodeProject
As you've probably worked out, you need to use FTP. The .net framework has some methods which will help you here, in particular System.Net.FtpWebRequest. The general sequence of events that you need to do is:

    • Create an FtpWebRequest that contains the URL of your file. It would appear that you need to prefix it with ftp://

    • If you want to use a username and password, set the Credentials property to a NetworkCredential instance

    • Set the UseBinary property of the FtpWebRequest to true, so that any binary files don't get treated as text

    • Set the Method property of the FtpWebRequest to WebRequestMethods.Ftp.DownloadFile

From here, you can either go synchronously or asynchronously. The latter option is more complex, but will not hold up your code while it runs. Synchronous is simpler, but will be slower for large files. The general sequence of events is:

    • Use GetResponse or BeginGetResponse/EndGetResponse to get the FtpWebResponse object

    • Get the response stream from the FtpWebResponse object

    • Copy data from the response stream to the stream representing the file, or use a StreamReader instance if you just want to directly read it

Share this answer
blackbanjo 11-Jun-10 16:49pm    
Well, I am 100% self-taught so I see where you're going, but I can't quite grasp the concept fully. What I have is this:

Dim URI As String = host & remoteDirectory & filename
Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)

ftp.Credentials = New System.Net.NetworkCredential(FTPusername, FTPpassword)

ftp.KeepAlive = False

ftp.UseBinary = True

ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream

Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0
End Using
End Using
End Using
End Sub

This downloads the file, but like I mentioned earlier I am trying to avoid this. Am I on the right path? Where next? I know I am missing a super obvious step but I just can't find it.
0x3c0 11-Jun-10 17:07pm    
Yes, you're pretty much there. You've got the response stream, now you just need to (I assume you still want to manipulate text) create a System.IO.StreamReader, passing responseStream to the constructor. Then, you can use ReadLine and the associated methods just like you would a normal file
blackbanjo 11-Jun-10 18:58pm    
Thanks, I got it working. I get an error, but oddly enough it really doesn't impede anything and all the data streams just fine. This is what I came up with:

Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
'loop to read & write to file
' Try

Dim sr As New System.IO.StreamReader(responseStream)

line = sr.ReadLine()
strCreds = strCreds & line
Loop Until line Is Nothing
Catch ex As Exception
'dont do anything, dont know how to fix this error
'just catch the error and move on
End Try


End Using


End Using

Again, thanks so much.
0x3c0 12-Jun-10 4:44am    
You're welcome. I suspect that the error comes from the last concatenation of the line variable. At that point, line is Nothing, so it may simply be that you get a NullReferenceException. There are also a few pieces of redundant code - when you close the StreamReader, the underlying Stream is also closed. Beyond that, the stream is closed when it gets Dispose called, as your Using block will do. It won't cause any massive errors, but it's a little inefficient.

A slightly better loop, and one which is less likely to cause an error is:
line = sr.ReadLine()
Do While line IsNot Nothing
strCreds = strCreds & line ' If you wanted better performance and more efficient memory usage, you could use a StringBuilder's Append method
line = sr.ReadLine()
Donnavan 26-Jul-10 19:02pm    
0x3c0 is right about the loop
Another way this could be done without errors is by changing the loop as follows:

While Not sr.EndOfStream
line = sr.ReadLine()
End While

*Thanks I've been looking for how to accomplish the same task for some time now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900