【问题标题】:Upload files to a SFTP server with VB.NET (Visual Basic)使用 VB.NET (Visual Basic) 将文件上传到 SFTP 服务器
【发布时间】:2020-04-18 18:21:24
【问题描述】:

我正在创建一个允许您将文件发送到共享服务器的程序。不幸的是,我通过将程序上传到 FTP 服务器来构建程序,但我意识到连接不安全,并且使用网络数据包管理器可以轻松找到我服务器的用户名和密码。因此,我选择了 SFTP 服务器,它应该是 FTP 的安全版本。不幸的是我不能使用它。
我尝试了这些代码,它们可以完美地与 ftp 一起使用,但不能与 sftp 一起使用。

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")

我再说一遍,两者都适用于 ftp,但不适用于 sftp。他们给出了这个错误。 “不支持前缀”或类似的东西。 我尝试使用 Renci 的 SSH.NET 外部库

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

但我不能使用事件 FileUploadCompleted 或 ProgressChanged,我需要它!
我将“WithEvents”放在 var 名称之前,但没有任何改变。
请帮帮我!


(对不起,我的英语不好,编程能力差)
谢谢!

【问题讨论】:

  • 参见 UploadFile 方法重载或 BeginUploadFile 方法。两者都允许指定通知操作进度和终止的回调(与 DownloadFile 和 BeginDownloadFile 方法相同)。
  • 吉米 我该怎么做?你能给我一个例子吗?

标签: .net vb.net


【解决方案1】:

我在我的项目中使用此代码。请检查一下。

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

【讨论】:

    猜你喜欢
    • 2018-07-11
    • 2016-01-28
    • 2022-11-10
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多