简体   繁体   中英

Upload files to a SFTP server with VB.NET (Visual Basic)

I am creating a program that allows you to send files to a shared server. Unfortunately I built the program by uploading it to an FTP server, but I realized that the connection is not secure and with a network packet manager you could easily find my server's username and password. I therefore opted for an SFTP server which should be the secure version of FTP. Unfortunately I can't use it.
I tried with these codes, which work perfectly with ftp but with sftp not.

My.Computer.Network.UploadFile("C:\Path\file.txt", "sftp://server.net/path/file.txt", "username", "password")

Dim WithEvents upload as New WebClient
upload.uploadfileasync("C:\path","sftp://adress")

I repeat, both works with ftp, but not with sftp. They gave this error. "prefix is not supported" or something similar. I tryed to use Renci's SSH.NET external library

Dim WithEvents client As Renci.SshNet.SftpClient = New Renci.SshNet.SftpClient("website", "username", "password")
    client.Connect()
    Using stream As Stream = File.OpenRead("C:\Path")
        client.UploadFile(stream, "/path")
    End Using

But i can't use the event FileUploadCompleted or ProgressChanged and i need it!
i putted "WithEvents" before the name of the var but nothing changed.
Please help me!


(Sorry for my bad english and my poor programming skills)
Thanks!

am using this code in my project. please check it.

Public Function UploadFile(strFileLocalFullPath As String, strRemoteUploadPath As String) As Boolean
            Try
                Using session As New Session()
                    session.SessionLogPath = "your log path"
                    session.Open(sessionOptions)
                    Dim transferOptions As New TransferOptions()
                    transferOptions.TransferMode = TransferMode.Binary
                    transferOptions.FilePermissions = Nothing
                    transferOptions.PreserveTimestamp = False
                    transferOptions.ResumeSupport.State = TransferResumeSupportState.Off

                    Dim transferResult As TransferOperationResult
                    transferResult = session.PutFiles(strFileLocalFullPath, strRemoteUploadPath, False, transferOptions)
                    transferResult.Check()
                End Using
            Catch ex As Exception
                'Log Exception here
                Return False
            End Try

            Return True
        End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM