1
使用WinSCP赋予我已经通过SFTP发送的文件VB.NET中使用WinSCP赋予另一台服务器。如何检查是否到SFTP服务器的连接是成功的VB.NET
我想看看如果连接成功与否。
我还需要知道,如果该文件中的目录已经存在事前。
使用WinSCP赋予我已经通过SFTP发送的文件VB.NET中使用WinSCP赋予另一台服务器。如何检查是否到SFTP服务器的连接是成功的VB.NET
我想看看如果连接成功与否。
我还需要知道,如果该文件中的目录已经存在事前。
有几件事你可以做。你可以创建一个会话日志,告诉你(在很多细节上)你的文件传输过程中发生了什么。您还可以在mySession.Open(mySessionOptions)
附近放置一个try-catch块来捕获错误。
最后,使用mySession.FileExists(remotepath)
来检查,看看是否该文件已经在服务器上。
Dim mySessionOptions As New SessionOptions
With mySessionOptions
.Protocol = Protocol.Sftp
.HostName = "999.999.999.999"
.UserName = "login"
.Password = "mypassword"
.SshHostKeyFingerprint = "ssh-dss 1024 99:87:99:4d:99:a3:99:b9:99:15:99:f2:99:87:88:b2"
End With
Using mySession As Session = New Session
' Will continuously report progress of synchronization
AddHandler mySession.FileTransferred, AddressOf FileTransferred
' Connect
mySession.SessionLogPath = "C:\Users\yourName\yourFolder\Sessionlog.log"
'Use Try-Catch to check for error in connection
Try
mySession.Open(mySessionOptions)
Catch ex As Exception
MessageBox.show(ex.Message)
mySession.Close()
Exit Sub
End Try
'Check to see if file exist already on server
If mySession.FileExists(remotePath) Then
MessageBox.Show("File Exists on Server")
mySession.Close()
Exit Sub
End If
mySession.PutFiles("C:\Users\yourName\yourFolder\yourfile.dat", remotePath)
mySession.Close()
End Using
请记住检查您创建的日志,以确切了解发生了什么。
您还需要测试'PutFiles'是否成功。使用'.Check()'。例如:'mySession.PutFiles(“C:\ Users \ yourName \ yourFolder \ yourfile.dat”,remotePath).Check()'。请参阅[捕获操作结果](https://winscp.net/eng/docs/library_session#results)。 –