title: Upload Multiple FTP Files
body: <p>Requirement, upload 1500 jpg images every night, the below code opens and closes a connection many times, I'm wondering if there is a better way.</p>
  
  <p>...this is a code snippet, so there are variables here that are defined elsewhere</p>
  
  <pre><code>Dim picClsRequest = DirectCast(System.Net.WebRequest.Create(ftpImagePath), System.Net.FtpWebRequest)
  Dim picClsStream As System.IO.Stream
  
  Dim picCount As Integer = 0
  For i = 1 To picPath.Count - 1
      picCount = picCount + 1
      log("Sending picture (" &amp; picCount &amp; " of " &amp; picPath.Count &amp; "):" &amp; picDir &amp; "/" &amp; picPath(i))
      picClsRequest = DirectCast(System.Net.WebRequest.Create(ftpImagePath &amp; "/" &amp; picPath(i)), System.Net.FtpWebRequest)
      picClsRequest.Credentials = New System.Net.NetworkCredential(ftpUsername, ftpPassword)
      picClsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
      picClsRequest.UseBinary = True
      picClsStream = picClsRequest.GetRequestStream()
  
      bFile = System.IO.File.ReadAllBytes(picDir &amp; "/" &amp; picPath(i))
      picClsStream.Write(bFile, 0, bFile.Length)
      picClsStream.Close()
  
  Next
  </code></pre>
  
  <p>Some comments:</p>
  
  <p>Yes, I know picCount is redundant...It was late at night.</p>
  
  <p>ftpImagePath, picDir, ftpUsername, ftpPassword are all variables</p>
  
  <p>Yes, this is unencrypted</p>
  
  <p>This code works fine, I'm looking to optimize</p>
  
  <p>Related Question: <a href="https://stackoverflow.com/questions/1772654/ftp-upload-multiple-files-without-disconnect-using-net">FTP Upload multiple files without disconnect using .NET</a></p>