SFTP VB Net come cancellare un file
Tempo di lettura: min.
Per poter realizzare una funzione che permetta di cancellare un file su un SFTP server abbiamo utilizzato la libreria SharpSSH.
Per sapere cos'è SharpSSH
SharpSSH is a pure .NET implementation of the SSH2 client protocol suite. It provides an API for communication with SSH servers and can be integrated into any .NET application.
The library is a C# port of the JSch project from JCraft Inc. and is released under BSD style license.
SharpSSH allows you to read/write data and transfer files over SSH channels using an API similar to JSch's API. In addition, it provides some additional wrapper classes which offer even simpler abstraction for SSH communication.
fonte SharpSSH
Il codice che abbiamo dovuto sviluppare per permettere l'eliminazione di un file su server SFTP è stato il seguente.
Generazione della classe per che derivante da SshTransferProtocolBase
Public Class SshTransfer
Inherits SshTransferProtocolBase
Public Sub New(ByVal sftpHost As String, ByVal user As String, ByVal password As String)
MyBase.New(sftpHost, user, password)
End Sub
Public Overrides Sub Cancel()
Throw New Exception("The method or operation is not implemented.")
End Sub
Protected Overrides ReadOnly Property ChannelType As String
Get
Return "sftp"
End Get
End Property
Public Overrides Sub [Get](fromFilePath As String, toFilePath As String)
Throw New Exception("The method or operation is not implemented.")
End Sub
Public Overrides Sub Mkdir(directory As String)
sftpChannel.mkdir(directory)
End Sub
Public Overrides Sub Put(fromFilePath As String, toFilePath As String)
Throw New Exception("The method or operation is not implemented.")
End Sub
Public Function sftpChannel() As ChannelSftp
Return CType(m_channel, SharpSsh.jsch.ChannelSftp)
End Function
Public Function Delete(ByVal path As String) As Boolean
Dim bRet As Boolean = False
Try
sftpChannel.rm(path)
bRet = True
Catch ex As Exception
End Try
Return bRet
End Function
End Class
Per poter utilizzare questa classe e quindi realizzare il metodo DeleteFile abbiamo creato questa funzione
Public Function DeleteFile(ByVal DeleteFilePath As String, ByRef sErrorSftp As String) As Boolean
Dim bRet As Boolean = False
Dim sshTran As SshTransfer = Nothing
Try
sshTran = New SshTransfer(sHostSFtp, sNomeUtenteSFtp, sPasswordSFtp)
sshTran.Connect()
If Not sshTran.Delete(DeleteFilePath) Then Throw New Exception("Errore cancellazione del file")
bRet = True
Catch ex As Exception
sErrorSftp = "Errore durante la download " & vbCrLf & ex.Message & vbCrLf & ex.StackTrace
Finally
If Not IsNothing(sshTran) Then sshTran.Close()
sshTran = Nothing
If Not IsNothing(sshCp) Then sshCp.Close()
sshCp = Nothing
End Try
Return bRet
End Function
Aiuta MrPaloma.com a crescere, utilizza uno dei seguenti link per iscriverti a qualche sito così che noi possiamo ricevere un omaggio.
Ricordiamo che in qualità di Affiliato Amazon riceviamo un guadagno dagli acquisti idonei.
Per sapere cos'è SharpSSH
SharpSSH is a pure .NET implementation of the SSH2 client protocol suite. It provides an API for communication with SSH servers and can be integrated into any .NET application.
The library is a C# port of the JSch project from JCraft Inc. and is released under BSD style license.
SharpSSH allows you to read/write data and transfer files over SSH channels using an API similar to JSch's API. In addition, it provides some additional wrapper classes which offer even simpler abstraction for SSH communication.
fonte SharpSSH
Il codice che abbiamo dovuto sviluppare per permettere l'eliminazione di un file su server SFTP è stato il seguente.
Generazione della classe per che derivante da SshTransferProtocolBase
Public Class SshTransfer
Inherits SshTransferProtocolBase
Public Sub New(ByVal sftpHost As String, ByVal user As String, ByVal password As String)
MyBase.New(sftpHost, user, password)
End Sub
Public Overrides Sub Cancel()
Throw New Exception("The method or operation is not implemented.")
End Sub
Protected Overrides ReadOnly Property ChannelType As String
Get
Return "sftp"
End Get
End Property
Public Overrides Sub [Get](fromFilePath As String, toFilePath As String)
Throw New Exception("The method or operation is not implemented.")
End Sub
Public Overrides Sub Mkdir(directory As String)
sftpChannel.mkdir(directory)
End Sub
Public Overrides Sub Put(fromFilePath As String, toFilePath As String)
Throw New Exception("The method or operation is not implemented.")
End Sub
Public Function sftpChannel() As ChannelSftp
Return CType(m_channel, SharpSsh.jsch.ChannelSftp)
End Function
Public Function Delete(ByVal path As String) As Boolean
Dim bRet As Boolean = False
Try
sftpChannel.rm(path)
bRet = True
Catch ex As Exception
End Try
Return bRet
End Function
End Class
Per poter utilizzare questa classe e quindi realizzare il metodo DeleteFile abbiamo creato questa funzione
Public Function DeleteFile(ByVal DeleteFilePath As String, ByRef sErrorSftp As String) As Boolean
Dim bRet As Boolean = False
Dim sshTran As SshTransfer = Nothing
Try
sshTran = New SshTransfer(sHostSFtp, sNomeUtenteSFtp, sPasswordSFtp)
sshTran.Connect()
If Not sshTran.Delete(DeleteFilePath) Then Throw New Exception("Errore cancellazione del file")
bRet = True
Catch ex As Exception
sErrorSftp = "Errore durante la download " & vbCrLf & ex.Message & vbCrLf & ex.StackTrace
Finally
If Not IsNothing(sshTran) Then sshTran.Close()
sshTran = Nothing
If Not IsNothing(sshCp) Then sshCp.Close()
sshCp = Nothing
End Try
Return bRet
End Function
Salsomaggiore Terme (Programmazione) -
03/11/2012 - SFTP VB Net come cancellare un file
Written by Mokik
Written by Mokik
Link referral
Amazon Sostieni MrPaloma facendo acquisti su Amazon partendo da questo link amazon.it.
Plutus Ricevi uno sconto del 100% in PLU su tutti i vantaggi basati su abbonamento. plutus.it.
Ricordiamo che in qualità di Affiliato Amazon riceviamo un guadagno dagli acquisti idonei.