2012-12-16 128 views
2

我需要我的程序通過指定FTP用戶名和密碼通過FTP下載大文件。
此外它必須有「暫停」功能,這就是爲什麼我需要我的程序來獲取文件的部分。 我使用VB.NET和.NET 3.5框架。有什麼辦法可以做到嗎?使用VB.NET通過FTP下載文件零件

+1

下載WinSCP賦予你需要使用一些第三方FTP/FTPS客戶端組件爲.NET這一點。我可以提及Rebex.NET和我們的SecureBlackbox,還有其他可用的。 –

+1

我知道Rebex,但我認爲沒有Rebex是可能的。我發現它可能與System.Net.FtpWebRequest。但我不知道該怎麼做。 – Jet

回答

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

這對我的作品。

+2

我想你已經寫了這個代碼上傳到服務器,而不是下載。你已經使用了System.Net.WebRequestMethods.Ftp.UPLOADFILE方法。 – Jet

1

從NuGet包

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