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()

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.:)

Aww, it doesn't :<

Only uploads the first file, ignores the rest.

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)

Seems to work perfectly so far :O

Thanks man :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.