Jet
Jet

Reputation: 526

Download file parts via FTP using VB.NET

I need my program to download large files via FTP by specifying FTP username and password.
Also it must have "Pause" function, that's why I need my program to get file parts. I use VB.NET and .NET 3.5 Framework. Is there any way to do it?

Upvotes: 2

Views: 4532

Answers (2)

Brajesh
Brajesh

Reputation: 11

Download WinSCP from NuGet Package

Import WinSCP <br>
Public Class fmbFtp <br>

    Private Sub btnDownload_Click(sender As Object, e As EventArgs) Handles btnDownload.Click

        Try

            ' Setup session options

            Dim sessionOptions As New SessionOptions

            With sessionOptions
                .Protocol = Protocol.Sftp
                .HostName = "example.com"
                .UserName = "UserName"
                .Password = "Password"
                'telnet command to get key
                'ssh-keygen - l - f / etc / ssh / ssh_host_rsa_key
                .SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"

            End With

            Using session As New Session

                ' Connect

                session.Open(sessionOptions)

                ' Upload files

                Dim transferOptions As New TransferOptions
                transferOptions.TransferMode = TransferMode.Binary

                Dim transferResult As TransferOperationResult
                transferResult = session.GetFiles("/path/filename.txt", My.Computer.FileSystem.CurrentDirectory)
                ' Throw on any error
                transferResult.Check()
                ' Print results
                For Each transfer In transferResult.Transfers
                    Console.WriteLine("Upload of {0} succeeded", transfer.FileName)
                Next
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

Upvotes: 1

Ish Goel
Ish Goel

Reputation: 315

You can use FtpWebRequest like this :

Dim request As System.Net.FtpWebRequest =   DirectCast(System.Net.WebRequest.Create("FTP Path along with destination file name"), System.Net.FtpWebRequest)
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile

request.Credentials = New System.Net.NetworkCredential("username", "password")

Dim file() As Byte = System.IO.File.ReadAllBytes("path of file to be copied")

Dim strz As System.IO.Stream = request.GetRequestStream()
strz.Write(file, 0, file.Length)
strz.Close()
strz.Dispose()

This works for me.

Upvotes: 0

Related Questions