-2

I want to download something from my FTP server, not very sure how to do it. I read many threads online but none of them seems to be working for me.

i know that

"Client.DownloadFile("http://www.dajialol.com/box/League of Legends.exe", @"C:\Users\MacBook\Desktop\League of Legends.exe");"

will download something that is not in ftp. But im not very sure how will i download from ftp.

I know you have to put in your ftp username and password, etc.

private void btnzoom_Click(object sender, EventArgs e)
        {
            WebClient Client = new WebClient ();

            Client.DownloadFile("http://www.dajialol.com/box/League of Legends.exe", @"C:\Users\MacBook\Desktop\League of Legends.exe");

        }
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Rain
  • 101
  • 1
  • 1
  • 8
  • try these similar questions http://stackoverflow.com/questions/6098694/read-file-from-ftp?lq=1 and http://stackoverflow.com/questions/2781654/ftpwebrequest-download-file?rq=1 – Amitd Mar 11 '14 at 17:44
  • duplicate of http://stackoverflow.com/questions/19124633/c-sharp-ftp-upload-and-download – Steve Mar 11 '14 at 17:44
  • @Steve Thx, I do find my answer in those links – Rain Mar 11 '14 at 18:15

2 Answers2

0

It looks like some examples on the documentation for FtpWebRequest make use of WebClient:

WebClient request = new WebClient();
request.Credentials = new NetworkCredential("anonymous", "janeDoe@contoso.com");
byte[] newFileData = request.DownloadData(serverUri.ToString());

The main thing to note is that serverUri.Scheme is Uri.UriSchemeFtp, so it seems that WebClient can make ftp:// requests.

Another alternative I've used in the past for ease-of-use is somebody's FtpClient class on GitHub:

var client = new Ftp("ftp://hostname.com", "username", "password");
client.DownloadFile("remotefile.txt", "localfile.txt");
David
  • 208,112
  • 36
  • 198
  • 279
0

Check out my FTP class: Its pretty straight forward.

Take a look at my FTP class, it might be exactly what you need.

Public Class FTP
        '-------------------------[BroCode]--------------------------
        '----------------------------FTP-----------------------------
        Private _credentials As System.Net.NetworkCredential
        Sub New(ByVal _FTPUser As String, ByVal _FTPPass As String)
            setCredentials(_FTPUser, _FTPPass)
        End Sub
        Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String)
            Dim _FileInfo As New System.IO.FileInfo(_FileName)
            Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
            _FtpWebRequest.Credentials = _credentials
            _FtpWebRequest.KeepAlive = False
            _FtpWebRequest.Timeout = 20000
            _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
            _FtpWebRequest.UseBinary = True
            _FtpWebRequest.ContentLength = _FileInfo.Length
            Dim buffLength As Integer = 2048
            Dim buff(buffLength - 1) As Byte
            Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
            Try
                Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
                Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
                Do While contentLen <> 0
                    _Stream.Write(buff, 0, contentLen)
                    contentLen = _FileStream.Read(buff, 0, buffLength)
                Loop
                _Stream.Close()
                _Stream.Dispose()
                _FileStream.Close()
                _FileStream.Dispose()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Upload Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
        Public Sub DownloadFile(ByVal _FileName As String, ByVal _ftpDownloadPath As String)
            Try
                Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpDownloadPath)
                _request.KeepAlive = False
                _request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
                _request.Credentials = _credentials
                Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
                Dim responseStream As System.IO.Stream = _response.GetResponseStream()
                Dim fs As New System.IO.FileStream(_FileName, System.IO.FileMode.Create)
                responseStream.CopyTo(fs)
                responseStream.Close()
                _response.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Download Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
        Public Function GetDirectory(ByVal _ftpPath As String) As List(Of String)
            Dim ret As New List(Of String)
            Try
                Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpPath)
                _request.KeepAlive = False
                _request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
                _request.Credentials = _credentials
                Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
                Dim responseStream As System.IO.Stream = _response.GetResponseStream()
                Dim _reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)
                Dim FileData As String = _reader.ReadToEnd
                Dim Lines() As String = FileData.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
                For Each l As String In Lines
                    ret.Add(l)
                Next
                _reader.Close()
                _response.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Directory Fetch Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            Return ret
        End Function

        Private Sub setCredentials(ByVal _FTPUser As String, ByVal _FTPPass As String)
            _credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
        End Sub
    End Class

To initialize:

Dim ftp As New FORM.FTP("username", "password")

ftp.UploadFile("c:\file.jpeg", "ftp://domain/file.jpeg")

ftp.DownloadFile("c:\file.jpeg", "ftp://ftp://domain/file.jpeg")

Dim directory As List(Of String) = ftp.GetDirectory("ftp://ftp.domain.net/")
        ListBox1.Items.Clear()
        For Each item As String In directory
            ListBox1.Items.Add(item)
        Next
THE AMAZING
  • 1,496
  • 2
  • 16
  • 38