Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports Microsoft.VisualBasic
- Imports WinSCP
- 'This class is used to store user input values for the session options of the
- 'SFTP connection, DirectCast is being called to convert strings to their respective Enum
- 'Values
- Public Class SFTPConnectorManager
- Private sProtocolOptions As String
- Private sFtpMode As String
- Private sFtpSecure As String
- Private sHostName As String
- Private sSSHKey As String
- Private sUserName As String
- Private sPassword As String
- Private iPort As Integer
- Private bWebDAV As Boolean
- Private bUnsecure As Boolean
- Property ProtocolSelection() As String
- Set(ByVal Value As String)
- If (Value = "Sftp" Or Value = "0") Then
- sProtocolOptions = Value
- ElseIf (Value = "Scp" Or "1") Then
- sProtocolOptions = Value
- ElseIf (Value = "Ftp" Or "2") Then
- sProtocolOptions = Value
- ElseIf (Value = "Webdav" Or "3") Then
- sProtocolOptions = Value
- Else
- sProtocolOptions = ""
- End If
- End Set
- Get
- ' Return DirectCast([Enum].Parse(GetType(Protocol), sProtocolOptions), Protocol)
- If (sProtocolOptions = "Sftp" Or "0") Then
- Return Protocol.Sftp
- ElseIf (sProtocolOptions = "Scp" Or "1") Then
- Return Protocol.Scp
- ElseIf (sProtocolOptions = "Ftp" Or "2") Then
- Return Protocol.Ftp
- ElseIf (sProtocolOptions = "Webdav" Or "3") Then
- Return Protocol.Webdav
- Else
- Return False
- End If
- End Get
- End Property
- Property FtpModeSelection() As String
- Set(ByVal value As String)
- If (value = "Passive") Then
- sFtpMode = value
- ElseIf (value = "Active") Then
- sFtpMode = value
- Else
- sFtpMode = ""
- End If
- End Set
- Get
- ' Return DirectCast(Protocol.Parse(GetType(FtpMode), sFtpMode), Protocol)
- If (sFtpMode = "Passive" Or "0") Then
- Return FtpMode.Passive
- ElseIf (sFtpMode = "Active" Or "1") Then
- Return FtpMode.Active
- Else
- Return False
- End If
- End Get
- End Property
- Property FtpSecure() As String
- Set(ByVal value As String)
- If (value = "FtpSecure.None") Then
- sFtpSecure = value
- ElseIf (value = "FtpSecure.Implicit") Then
- sFtpSecure = value
- ElseIf (value = "FtpSecure.Explicit") Then
- sFtpSecure = value
- Else
- sFtpSecure = ""
- End If
- End Set
- Get
- Return sFtpSecure
- End Get
- End Property
- Property HostName() As String
- Set(ByVal value As String)
- If (Uri.CheckHostName(value) = UriHostNameType.IPv4) Then
- sHostName = value
- Else
- sHostName = ""
- End If
- End Set
- Get
- Return sHostName
- End Get
- End Property
- Property Port() As String
- Set(ByVal value As String)
- If (value > 0 & value < 65535) Then
- iPort = value
- Else
- iPort = 21
- End If
- End Set
- Get
- Return iPort
- End Get
- End Property
- Property UserName() As String
- Set(ByVal value As String)
- sUserName = value
- End Set
- Get
- Return sUserName
- End Get
- End Property
- Property Password() As String
- Set(ByVal value As String)
- sPassword = value
- End Set
- Get
- Return sPassword
- End Get
- End Property
- Property WebDav() As Boolean
- Set(ByVal value As Boolean)
- bWebDAV = value
- End Set
- Get
- Return bWebDAV
- End Get
- End Property
- Property Unsecure() As Boolean
- Set(ByVal value As Boolean)
- bUnsecure = value
- End Set
- Get
- Return bUnsecure
- End Get
- End Property
- Property SSHKey() As String
- Set(ByVal value As String)
- sSSHKey = value
- End Set
- Get
- Return sSSHKey
- End Get
- End Property
- Function StartConnection(eProtocolOptions, sHostName, sUserName, sPassword, sSSHKey)
- Try
- Dim oSessionOptions As New SessionOptions
- With oSessionOptions
- .Protocol = eProtocolOptions
- .HostName = sHostName
- .UserName = sUserName
- .Password = sPassword
- .SshHostKeyFingerprint = sSSHKey
- End With
- Using oSession As New Session
- oSession.Open(oSessionOptions)
- Dim oTransferOptions As New TransferOptions
- oTransferOptions.TransferMode = TransferMode.Binary
- Dim oTransferResult As TransferOperationResult
- oTransferResult = oSession.GetFiles("/root/testfile.txt", "C:\Users\dale\", False, oTransferOptions)
- oTransferResult.Check()
- Dim FormObjects = New Form1
- Dim TextBox = FormObjects.StatusTextBox1
- For Each transfer In oTransferResult.Transfers
- TextBox.Text = "Download of {0} succeeded" + transfer.FileName
- Next
- End Using
- Return 0
- Catch ex As Exception
- MessageBox.Show(ex.ToString())
- Return 1
- End Try
- End Function
- End Class
Add Comment
Please, Sign In to add comment