使用VB.NET通过FTP下载文件部分

时间:2012-12-16 08:00:19

标签: vb.net ftp download

我需要我的程序通过FTP指定FTP用户名和密码来下载大文件 此外,它必须具有“暂停”功能,这就是为什么我需要我的程序来获取文件部分。 我使用VB.NET和.NET 3.5 Framework。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:1)

从NuGet Package下载WinSCP

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

答案 1 :(得分:0)

您可以像这样使用FtpWebRequest:

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()

这对我有用。