Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi i need help with this code to upload one exe to multiple ftp servers.
i have a file with all the links i need and user names and passwords , now it only uploads the first time every on after that gets the error invalid uri even though its correct.

Imports System.Net
Imports System.IO
Imports System.Data.SqlClient
Public Class Formftp

  Private Sub Buttonftp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonftp.Click
    Dim locat, combind, str As String
    locat = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)

    combind = System.IO.Path.Combine(locat, "FTPstr.cfg")

    Dim reader As New StreamReader(combind)

    str = reader.ReadToEnd()

    Dim TestArray() As String = str.Split(",")
    Dim strftp As String
    Dim s As Boolean

    For t As Integer = 0 To TestArray.Length - 1
      strftp = TestArray(t).ToString()
      Dim TestFTPArray() As String = strftp.Split("/")

      ' make a reference to a directory
      Dim di As New IO.DirectoryInfo(locat + "\ftp")
      Dim diar1 As IO.FileInfo() = di.GetFiles()
      Dim dra As IO.FileInfo

      'list the names of all files in the specified directory

      For Each dra In diar1

          Dim struri As String = String.Format("ftp://{0}/{1}/{2}", TestFTPArray(0), TestFTPArray(1), dra)

          Dim clsRequest As System.Net.FtpWebRequest = _
           DirectCast(System.Net.WebRequest.Create(struri), System.Net.FtpWebRequest)

          clsRequest.Credentials = New System.Net.NetworkCredential(TestFTPArray(2), TestFTPArray(3))
          clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

          ' read in file...
          Dim st As String = String.Format("{0}{1}{2}", locat, "\ftp\", dra)
          Dim bFile() As Byte = System.IO.File.ReadAllBytes(st)

          ' upload file...
          Dim clsStream As System.IO.Stream = _
          clsStream.Write(bFile, 0, bFile.Length)

        Catch ex As Exception

        End Try


        ' set up request...

  End Sub
End Class

update 2012/07/30:
i tested with one exe(trying to put it to multiple servers) it uploads it to the first ftp server then when connecting to the second and every ftp server after it it says "Invalid URI: The hostname could not be parsed." on the (
Dim clsRequest As System.Net.FtpWebRequest = _
           DirectCast(System.Net.WebRequest.Create(struri), System.Net.FtpWebRequest)
) line
even though i can use the exact same destination ftp://ip/folder/ in a browser ect with the username and password as is in the file it is reading and open the ftp folder in the browser. So there is no problem with the ftp folder itself , just the code doesn't want to ftp the exe to a second or third ftp server.
Updated 29-Jul-12 23:28pm
Dan Steuer 27-Jul-12 10:31am    
By "uploads the first time every on after that gets the error invalid uri even though its correct" do you mean it uploads the first file correctly but no other files upload or do you mean it uploads all the files to the first server but none of the other servers?
Christian Graus 27-Jul-12 17:16pm    
Post your exact error. If it says it is incorrect, it probably is.

1 solution

You should start with some basic debugging so you can ask a more specific question than 'fix my code'.

1) run it in a debugger. Trap on each FTP connect.. does it get there twice?

2) add some status prints to your code to see what it is trying to do.. how it is parsing your configuration.. etc.
Share this answer
dragoncwg 30-Jul-12 5:34am    
Thanks never saw as it was reading out of the file it adds a space infront of the server ip. Thanks

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