我需要我的程序通过指定 FTP 用户名和密码通过 FTP 下载大文件。
它还必须具有“暂停”功能,这就是为什么我需要我的程序来获取文件部分。我使用 VB.NET 和 .NET 3.5 框架。有什么办法吗?
问问题
4346 次
2 回答
1
从 NuGet 包下载 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
于 2018-01-18T02:57:32.277 回答
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()
这对我有用。
于 2013-01-16T14:58:34.863 回答