Question : simple VB file download example

I have looked at several examples here and on the internet, all of them seem they have a different purpose. Not to mention most of some kind of error in them. I would like to download one file in a small program each month. I want to fire it with a scheduled task each month on a server so a person doesn't have to babysit the program.

Answer : simple VB file download example

Well that is something completley different :)  I didn't notice any credential requirment so it was assumed you just wanted to download from an url location.  There is a couple of different ways to connect to a ftp I like using the FtpWebRequest() methods since it can give you a stream to manipulate.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Dim fs As FileStream
        Dim fwr As FtpWebRequest
        Dim data(4096) As Byte
        Dim bytesRead As Integer = 1

        Dim ftpUrl As String = "ftp://ftp.server.com:21/" 'ftp url + port
        Dim ftpUser As String = "username"
        Dim ftpPass As String = "password"
        Dim ftpFile As String = "TEST/myfile.txt" 'folder/file on server
        Dim savePath As String = "C:\myfile.txt" ' save location

        ftpUrl = ftpUrl.Insert(ftpUrl.Length, ftpFile)
        fwr = FtpWebRequest.Create(ftpUrl)
        fwr.Credentials = New NetworkCredential(ftpUser, ftpPass)
        fwr.Method = WebRequestMethods.Ftp.DownloadFile
        fs = New FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.Read)
        Do While bytesRead > 0
            bytesRead = fwr.GetResponse.GetResponseStream.Read(data, 0, data.Length)
            fs.Write(data, 0, bytesRead)
        Loop
        fs.Close()
        fs.Dispose()
        fs = Nothing
        fwr = Nothing
Random Solutions  
 
programming4us programming4us