' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.IO
Imports System.Net

Namespace E
    Class MainClass
        Public Shared Sub Upload(ByVal localfile As String, ByVal ftpurl As String, ByVal bin As Boolean)
            Dim req As FtpWebRequest = DirectCast(WebRequest.Create(ftpurl), FtpWebRequest)
            req.Method = WebRequestMethods.Ftp.UploadFile
            req.UseBinary = bin
            If bin Then
                Dim instm As Stream = New FileStream(localfile, FileMode.Open, FileAccess.Read)
                Dim outstm As Stream = req.GetRequestStream()
                Dim b As Byte() = New Byte(10000) {}
                Dim n As Integer
                n = instm.Read(b, 0, b.Length)
                While n > 0
                    outstm.Write(b, 0, n)
                    n = instm.Read(b, 0, b.Length)
                End While
                instm.Close()
                outstm.Close()
            Else
                Dim sr As StreamReader = New StreamReader(localfile)
                Dim sw As StreamWriter = New StreamWriter(req.GetRequestStream())
                Dim line As String
                line = sr.ReadLine()
                While line IsNot Nothing
                    sw.WriteLine(line)
                    line = sr.ReadLine()
                End While
                sr.Close()
                sw.Close()
            End If
            Dim resp As FtpWebResponse = DirectCast(req.GetResponse(), FtpWebResponse)
            Console.WriteLine(resp.StatusCode)
        End Sub
        Public Shared Sub Main(ByVal args As String())
            Upload("C:\X.java", "ftp://anonymous:xxxxxx@arnepc3/X.java", False)
            Upload("C:\X.class", "ftp://anonymous:xxxxxx@arnepc3/X.class", True)
        End Sub
    End Class
End Namespace
