Guest User

SFTPConnectorManager.vb

a guest
Jun 11th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.80 KB | None | 0 0
  1. Imports Microsoft.VisualBasic
  2. Imports WinSCP
  3. 'This class is used to store user input values for the session options of the
  4. 'SFTP connection, DirectCast is being called to convert strings to their respective Enum
  5. 'Values
  6.  
  7. Public Class SFTPConnectorManager
  8.     Private sProtocolOptions As String
  9.     Private sFtpMode As String
  10.     Private sFtpSecure As String
  11.     Private sHostName As String
  12.     Private sSSHKey As String
  13.     Private sUserName As String
  14.     Private sPassword As String
  15.     Private iPort As Integer
  16.     Private bWebDAV As Boolean
  17.     Private bUnsecure As Boolean
  18.  
  19.     Property ProtocolSelection() As String
  20.         Set(ByVal Value As String)
  21.             If (Value = "Sftp" Or Value = "0") Then
  22.                 sProtocolOptions = Value
  23.             ElseIf (Value = "Scp" Or "1") Then
  24.                 sProtocolOptions = Value
  25.             ElseIf (Value = "Ftp" Or "2") Then
  26.                 sProtocolOptions = Value
  27.             ElseIf (Value = "Webdav" Or "3") Then
  28.                 sProtocolOptions = Value
  29.             Else
  30.                 sProtocolOptions = ""
  31.             End If
  32.         End Set
  33.         Get
  34.             ' Return DirectCast([Enum].Parse(GetType(Protocol), sProtocolOptions), Protocol)
  35.             If (sProtocolOptions = "Sftp" Or "0") Then
  36.                 Return Protocol.Sftp
  37.             ElseIf (sProtocolOptions = "Scp" Or "1") Then
  38.                 Return Protocol.Scp
  39.             ElseIf (sProtocolOptions = "Ftp" Or "2") Then
  40.                 Return Protocol.Ftp
  41.             ElseIf (sProtocolOptions = "Webdav" Or "3") Then
  42.                 Return Protocol.Webdav
  43.             Else
  44.                 Return False
  45.             End If
  46.         End Get
  47.     End Property
  48.  
  49.     Property FtpModeSelection() As String
  50.         Set(ByVal value As String)
  51.             If (value = "Passive") Then
  52.                 sFtpMode = value
  53.             ElseIf (value = "Active") Then
  54.                 sFtpMode = value
  55.             Else
  56.                 sFtpMode = ""
  57.             End If
  58.         End Set
  59.         Get
  60.             ' Return DirectCast(Protocol.Parse(GetType(FtpMode), sFtpMode), Protocol)
  61.             If (sFtpMode = "Passive" Or "0") Then
  62.                 Return FtpMode.Passive
  63.             ElseIf (sFtpMode = "Active" Or "1") Then
  64.                 Return FtpMode.Active
  65.             Else
  66.                 Return False
  67.             End If
  68.         End Get
  69.     End Property
  70.  
  71.     Property FtpSecure() As String
  72.         Set(ByVal value As String)
  73.             If (value = "FtpSecure.None") Then
  74.                 sFtpSecure = value
  75.             ElseIf (value = "FtpSecure.Implicit") Then
  76.                 sFtpSecure = value
  77.             ElseIf (value = "FtpSecure.Explicit") Then
  78.                 sFtpSecure = value
  79.             Else
  80.                 sFtpSecure = ""
  81.             End If
  82.         End Set
  83.         Get
  84.             Return sFtpSecure
  85.         End Get
  86.     End Property
  87.  
  88.     Property HostName() As String
  89.         Set(ByVal value As String)
  90.  
  91.             If (Uri.CheckHostName(value) = UriHostNameType.IPv4) Then
  92.                 sHostName = value
  93.             Else
  94.                 sHostName = ""
  95.             End If
  96.         End Set
  97.         Get
  98.             Return sHostName
  99.         End Get
  100.     End Property
  101.  
  102.     Property Port() As String
  103.         Set(ByVal value As String)
  104.             If (value > 0 & value < 65535) Then
  105.                 iPort = value
  106.             Else
  107.                 iPort = 21
  108.             End If
  109.         End Set
  110.         Get
  111.             Return iPort
  112.         End Get
  113.     End Property
  114.  
  115.     Property UserName() As String
  116.         Set(ByVal value As String)
  117.             sUserName = value
  118.         End Set
  119.         Get
  120.             Return sUserName
  121.         End Get
  122.     End Property
  123.  
  124.     Property Password() As String
  125.         Set(ByVal value As String)
  126.             sPassword = value
  127.         End Set
  128.         Get
  129.             Return sPassword
  130.         End Get
  131.     End Property
  132.  
  133.     Property WebDav() As Boolean
  134.         Set(ByVal value As Boolean)
  135.             bWebDAV = value
  136.         End Set
  137.         Get
  138.             Return bWebDAV
  139.         End Get
  140.     End Property
  141.  
  142.     Property Unsecure() As Boolean
  143.         Set(ByVal value As Boolean)
  144.             bUnsecure = value
  145.         End Set
  146.         Get
  147.             Return bUnsecure
  148.         End Get
  149.     End Property
  150.  
  151.     Property SSHKey() As String
  152.         Set(ByVal value As String)
  153.             sSSHKey = value
  154.         End Set
  155.         Get
  156.             Return sSSHKey
  157.         End Get
  158.     End Property
  159.  
  160.     Function StartConnection(eProtocolOptions, sHostName, sUserName, sPassword, sSSHKey)
  161.         Try
  162.             Dim oSessionOptions As New SessionOptions
  163.             With oSessionOptions
  164.                 .Protocol = eProtocolOptions
  165.                 .HostName = sHostName
  166.                 .UserName = sUserName
  167.                 .Password = sPassword
  168.                 .SshHostKeyFingerprint = sSSHKey
  169.             End With
  170.  
  171.             Using oSession As New Session
  172.                 oSession.Open(oSessionOptions)
  173.  
  174.                 Dim oTransferOptions As New TransferOptions
  175.                 oTransferOptions.TransferMode = TransferMode.Binary
  176.  
  177.                 Dim oTransferResult As TransferOperationResult
  178.                 oTransferResult = oSession.GetFiles("/root/testfile.txt", "C:\Users\dale\", False, oTransferOptions)
  179.  
  180.                 oTransferResult.Check()
  181.  
  182.                 Dim FormObjects = New Form1
  183.                 Dim TextBox = FormObjects.StatusTextBox1
  184.  
  185.                 For Each transfer In oTransferResult.Transfers
  186.                     TextBox.Text = "Download of {0} succeeded" + transfer.FileName
  187.                 Next
  188.             End Using
  189.  
  190.             Return 0
  191.         Catch ex As Exception
  192.             MessageBox.Show(ex.ToString())
  193.             Return 1
  194.         End Try
  195.  
  196.     End Function
  197.  
  198. End Class
Add Comment
Please, Sign In to add comment