' 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.Globalization
Imports System.IO
Imports System.Text
Imports System.Net.Sockets
Imports System.Threading

Public Class FtpClient
    Private ctrl As TcpClient
    Private ctrlstm As NetworkStream
    Private data As TcpClient
    Private datastm As NetworkStream

    Private Shared Sub Send(ByVal stm As NetworkStream, ByVal line As String)
        Dim b As Byte() = Encoding.Default.GetBytes(line)
        stm.Write(b, 0, b.Length)
    End Sub

    Private Shared Function Receive(ByVal stm As NetworkStream) As String
        Dim b(100000 - 1) As Byte
        Dim ix As Integer = 0
        Dim n As Integer
        While stm.DataAvailable
            n = stm.Read(b, ix, b.Length - ix)
            ix += n
        End While
        Return Encoding.Default.GetString(b, 0, ix)
    End Function

    Public Shared Function Command(ByVal stm As NetworkStream, ByVal cmd As String) As String
        Send(stm, cmd + Convert.ToChar(13) + Convert.ToChar(10))
        Thread.Sleep(250)
        Return Receive(stm)
    End Function

    Private Sub SetupData()
        Dim dataaddr As String = Command(ctrlstm, "PASV")
        Dim addrparts As String() = dataaddr.Split("()".ToCharArray)(1).Split(",".ToCharArray)
        Dim datahost As String = addrparts(0) + "." + addrparts(1) + "." + addrparts(2) + "." + addrparts(3)
        Dim dataport As Integer = Integer.Parse(addrparts(4)) * 256 + Integer.Parse(addrparts(5))
        data = New TcpClient (datahost, dataport)
        datastm = data.GetStream
    End Sub

    Public Sub New(ByVal host As String, ByVal username As String, ByVal password As String)
        ctrl = New TcpClient (host, 21)
        ctrlstm = ctrl.GetStream
        Command(ctrlstm, "USER " + username)
        Command(ctrlstm, "PASS " + password)
    End Sub

    Public Function Dir() As String
        SetupData
        Command(ctrlstm, "LIST")
        Dim res As String = Receive(datastm)
        Receive(ctrlstm)
        datastm.Close
        data.Close
        Return res
    End Function

    Public Function ShortDir() As String
        SetupData
        Command(ctrlstm, "NLST")
        Dim res As String = Receive(datastm)
        Receive(ctrlstm)
        datastm.Close
        data.Close
        Return res
    End Function

    Public Sub MkDir(ByVal dir As String)
        Command(ctrlstm, "MKD " + dir)
    End Sub

    Public Sub RmDir(ByVal dir As String)
        Command(ctrlstm, "RMD " + dir)
    End Sub

    Public Sub ChDir(ByVal dir As String)
        Command(ctrlstm, "CWD " + dir)
    End Sub

    Public Sub UpLoad(ByVal filename As String, ByVal binary As Boolean)
        If binary Then
            Command(ctrlstm, "TYPE I")
        Else
            Command(ctrlstm, "TYPE A")
        End If
        SetupData
        Command(ctrlstm, "STOR " + filename)
        Receive(ctrlstm)
        Dim f As FileStream = New FileStream (filename, FileMode.Open)
        Dim b(100000 - 1) As Byte
        Dim ix As Integer = 0
        Dim n As Integer
        While ix < f.Length
            n = f.Read(b, 0, b.Length)
            datastm.Write(b, 0, n)
            Thread.Sleep(100)
            ix += n
        End While
        f.Close
        datastm.Close
        data.Close
    End Sub

    Public Sub DownLoad(ByVal filename As String, ByVal binary As Boolean)
        If binary Then
            Command(ctrlstm, "TYPE I")
        Else
            Command(ctrlstm, "TYPE A")
        End If
        SetupData
        Command(ctrlstm, "RETR " + filename)
        Receive(ctrlstm)
        Dim f As FileStream = New FileStream (filename, FileMode.Create)
        Dim b(100000 - 1) As Byte
        Dim n As Integer
        While datastm.DataAvailable
            n = datastm.Read(b, 0, b.Length)
            f.Write(b, 0, n)
            Thread.Sleep(100)
        End While
        f.Close
        datastm.Close
        data.Close
    End Sub

    Public Function Dir(ByVal filename As String) As String
        SetupData
        Command(ctrlstm, "LIST " + filename)
        Dim res As String = Receive(datastm)
        Receive(ctrlstm)
        datastm.Close
        data.Close
        Return res
    End Function

    Public Shared Function Eater(ByVal line As String, ByVal ix As Integer, ByVal n As Integer) As String
        Dim res As String = ""
        Dim line2() As Char = line.ToCharArray
        Dim ws As Integer = 0
        Dim i As Integer
        For i = 1 To (line2.Length - 1)
            If line2(i) = " " And line2(i-1) <> " " Then
                ws = ws + 1
            End If
            If ws >= ix And ws < (ix + n) Then
                res = res & line2(i)
            End If
        Next
        Eater = res.Trim
    End Function
    
    Public Shared Function ParseTime(ByVal line As String) As String
        ParseTime = Eater(line, 5, 3)
    End Function

    Public Function FileTime(ByVal filename As String) As DateTime
        Dim fmt() As String = { "MMM dd HH:mm", "MMM dd yyyy" }
        FileTime = DateTime.ParseExact(ParseTime(Dir(filename)), fmt, Nothing, DateTimeStyles.AllowWhiteSpaces)
    End Function

    Public Sub Logout()
        Command(ctrlstm, "QUIT")
        ctrlstm.Close
        ctrl.Close
    End Sub
End Class 

Class TestClass
    Public Shared Sub Main(ByVal args As String())
        Dim cli As FtpClient = New FtpClient ("localhost", "anonymous", "arne@")
        Console.WriteLine(cli.FileTime("test1.txt"))
        Console.WriteLine(cli.FileTime("test2.txt"))
        cli.Logout
    End Sub
End Class 
