954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Visual Basic ftp an entire folder with this code

Hey guys, I got this code to upload an FTP file which works great, but what do I do when I wnat to upload the contents of an entire folder? I mean, I could make a for-each but that would mean disconnecting and reconnecting to the FTP server alot of times which is just uneccesary. Any ideas?

Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://ftp.blabla.com/mybackups.zip"), System.Net.FtpWebRequest)
        request.Credentials = New System.Net.NetworkCredential("user", "pass")
        request.Method = System.Net.WebRequestMethods.Ftp.UploadFile

        Dim file() As Byte = System.IO.File.ReadAllBytes("c:\mybackups.zip")

        Dim strz As System.IO.Stream = request.GetRequestStream()
        strz.Write(file, 0, file.Length)
        strz.Close()
        strz.Dispose()
crankyslap
Newbie Poster
17 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Not really sure since I have not uploaded anything to FTP with vb.net, but could you just not use:

Dim file() As Byte = System.IO.File.ReadAllBytes("c:\mybackups.zip")
        Dim file2() As Byte = System.IO.File.ReadAllBytes("c:\mybackups2.zip")
        Dim file3() As Byte = System.IO.File.ReadAllBytes("c:\mybackups3.zip")

        Dim strz As System.IO.Stream = request.GetRequestStream()
        strz.Write(file, 0, file.Length)
        strz.Write(file2, 0, file.Length)
        strz.Write(file3, 0, file.Length)

Do let me know if that works.:)

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

Aww, it doesn't :<

Only uploads the first file, ignores the rest.

crankyslap
Newbie Poster
17 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Managed to find something simpler using My.Computer.Network.UploadFile .

Dim myFTPaddress As String = "ftp://codeorder.net/"
        Dim myFTPuserName As String = "your username here"
        Dim myFTPuserPassword As String = "your login password here"

        Dim myFiles() As String = {"C:\test1.png", "C:\test2.png", "C:\test3.png"} '// files for testing.
        Dim sTemp As String = Nothing '// used to extract only FileName with Extenstion.

        For Each mySelectedFile As String In myFiles '// loop thru your files.
            sTemp = IO.Path.GetFileName(mySelectedFile) '// get only the file name with extension from Full Path.
            '// upload File to website.
            My.Computer.Network.UploadFile(mySelectedFile, myFTPaddress & sTemp, myFTPuserName, myFTPuserPassword)
        Next

        MsgBox("File(s) Upload Successful.", MsgBoxStyle.Information)
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

Seems to work perfectly so far :O

Thanks man :D

crankyslap
Newbie Poster
17 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: