sftp/Module1.vb

Module Module1

    Dim objFTP As sfFTPLib.SFTPConnectionClass

    Sub Main()
        Dim objGlobal As sfFTPLib.[Global]
        'objGlobal = CreateObject("sfFTPLib.Global")
        objGlobal = New sfFTPLib.[Global]

        Dim strKey As String

        ' New license key format
        strKey = ""
        strKey = strKey & "<?xml version=\""1.0\"" ?>" & Chr(13) & Chr(10)
        strKey = strKey & "<License>" & Chr(13) & Chr(10)
        strKey = strKey & "<Version>2.0</Version>" & Chr(13) & Chr(10)
        strKey = strKey & "<Id>400012345</Id>" & Chr(13) & Chr(10)
        strKey = strKey & "<Name>name</Name>" & Chr(13) & Chr(10)
        strKey = strKey & "<Email>user@host.com</Email>" & Chr(13) & Chr(10)
        strKey = strKey & "<Company>company</Company>" & Chr(13) & Chr(10)
        strKey = strKey & "<Product>SmartFTP FTP Library</Product>" & Chr(13) & Chr(10)
        strKey = strKey & "<Features>" & Chr(13) & Chr(10)
        strKey = strKey & "<Feature>SFTP</Feature>" & Chr(13) & Chr(10)
        strKey = strKey & "</Features>" & Chr(13) & Chr(10)
        strKey = strKey & "<Users>1</Users>" & Chr(13) & Chr(10)
        strKey = strKey & "<Maintenance>2008-12-27</Maintenance>" & Chr(13) & Chr(10)
        strKey = strKey & "<Issue>2007-12-27</Issue>" & Chr(13) & Chr(10)
        strKey = strKey & "<Signature>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</Signature>" & Chr(13) & Chr(10)
        strKey = strKey & "</License>" & Chr(13) & Chr(10)

        ' Load License Key
        If objGlobal.LoadLicenseKeyData(strKey) Then
            System.Console.WriteLine("License key verified.")
        Else
            System.Console.WriteLine("Failed to verify license key.")
        End If

        Dim objUPnP As sfFTPLib.UPnPNATManager
        objUPnP = New sfFTPLib.UPnPNATManager
        objUPnP.Initialize()

        'objFTP = CreateObject("sfFTPLib.SFTPConnection")
        objFTP = New sfFTPLib.SFTPConnection

        ' Favorite settings
        objFTP.Host = "localhost"
        objFTP.Port = 22
        objFTP.Username = "username"
        objFTP.Password = "password"

        ' Proxy settings
        objFTP.Proxy.Type = sfFTPLib.enumProxyType.ftpProxyTypeNone

        objFTP.LogFile = "VBDemo.log"

        If objFTP.Connect() = sfFTPLib.enumError.ftpErrorSuccess Then
            ' Change Directory
            If objFTP.RealPath(".") = sfFTPLib.enumError.ftpErrorSuccess Then

                Dim strCurrentDirectory As System.String
                strCurrentDirectory = objFTP.LastPath

                ' Read Directory
                If objFTP.ReadDirectory(strCurrentDirectory) = sfFTPLib.enumError.ftpErrorSuccess Then
                    Dim objDirectory As sfFTPLib.FTPItemsClass
                    objDirectory = objFTP.Items
                    Dim objItem As sfFTPLib.FTPItem
                    For Each objItem In objDirectory
                        System.Console.WriteLine("Type={0}; Name={1}; Size={2}", objItem.Type, objItem.Name, objItem.Size)
                    Next
                Else
                    System.Console.WriteLine("ReadDirectory()failed. LastError = {0}", objFTP.LastError)
                End If

                ' Download File
                Dim nRestartLo As System.UInt32
                Dim nRestartHi As System.UInt32
                ' Convert value to UInt32
                nRestartLo = System.Convert.ToUInt32(0)
                nRestartHi = System.Convert.ToUInt32(0)

                If objFTP.DownloadFile("History.txt", "History.txt", nRestartLo, nRestartHi, nRestartLo, nRestartHi) = sfFTPLib.enumError.ftpErrorSuccess Then
                    System.Console.WriteLine("DownloadFile() successful.")
                    System.Console.WriteLine("LastTransferBytes = {0} B", objFTP.LastTransferBytes)
                    System.Console.WriteLine("LastTransferTime = {0} s", objFTP.LastTransferTime)
                    System.Console.WriteLine("LastTransferSpeed = {0} B/s", objFTP.LastTransferSpeed)
                Else
                    System.Console.WriteLine("DownloadFile() failed. LastError = {0}", objFTP.LastError)
                End If
            Else
                System.Console.WriteLine("ChangeDirectory() failed. LastError = {0}", objFTP.LastError)
            End If

            objFTP.Disconnect()
        Else
            If objFTP.LastError = sfFTPLib.enumError.ftpErrorLicense Then
                System.Console.WriteLine("Please acquire a license from http://www.smartftp.com")
            Else
                System.Console.WriteLine("Connect() failed. LastError = {0}", objFTP.LastError)
            End If
        End If

        objUPnP.Uninitialize()
        objFTP = Nothing
    End Sub
End Module
 
© SmartSoft Ltd.