How to: Download Files with FTP

Switch View :
ScriptFree
How to: Download Files with FTP

This sample shows how to download a file from an FTP server.

Example

C#
using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            Console.WriteLine(reader.ReadToEnd());

            Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
    
            reader.Close();
            response.Close();  
        }
    }
}
Compiling the Code

This example requires:

  • References to the System.Net namespace.

Community Content

Jung Kyu Lee
윈폼에서 접근할땐 안나던게 콘솔에서 접근하니까 권한없다고 나오면??
어떻게 처리해야합니꺄?~

gibbie99
Write to file
This should work:


StreamWriter writer = new StreamWriter("H:\temp.txt");
writer.Write(reader.ReadToEnd());

smannojkumar
Where the downloaded files will be saved
Where the downloaded files will be saved, when we use this download code

Thomas Lee
Sample using PowerShell
<#
.SYNOPSIS
This script used FTP to get and display a text file.
.DESCRIPTION
This script re-implements an MSDN Sample
.NOTESW
File Name : Get-FtpFile.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://pshscripts.blogspot.com/2010/09/get-ftpfileps1.html
MSDN sample posted tot:
http://msdn.microsoft.com/en-us/library/ms229711.aspx
.EXAMPLE
PSH [C:\foo]: .\Get-FtpFile.ps1'
This is Hello.Txt from www.reskit.net
Have a great day!

Download Complete, status:
226-Maximum disk quota limited to 100000 Kbytes
Used disk quota 78232 Kbytes, available 21767 Kbytes
226 Transfer complete.
#>
# Get the object used to communicate with the server.
$Request = [System.Net.WebRequest]::Create( "ftp://www.reskit.net/hello.txt");
$Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile

# This example assumes the FTP site uses anonymous logon.
# Username/password not real
$Request.Credentials = New-Object System.Net.NetworkCredential "UserName","Password"
$Response = $Request.GetResponse()
$ResponseStream = $Response.GetResponseStream()

# Read and display the text in the file
$Reader = new-object System.Io.StreamReader $Responsestream
[System.Console]::Writeline($Reader.ReadToEnd())

# Display Status
"Download Complete, status:"
$response.StatusDescription

# Close Reader and Response objects
$Reader.Close()
$Response.Close()

Thomas Lee
The C# sample does not work with binary files
The C# Sample shown here only works with text files. Files that are binary (e.g. ZIP files) will not work with this sample.

SPDowner
VB Example
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
    Public Class WebRequestGetExample
        Public Shared Sub Main()
            ' Get the object used to communicate with the server.
            Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://www.example.com/"), FtpWebRequest)
            request.Method = WebRequestMethods.Ftp.DownloadFile
            ' This example assumes the FTP site uses anonymous logon.
            request.Credentials = New NetworkCredential("anonymous", "example@example.com")
            Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
            Dim responseStream As Stream = response.GetResponseStream()
            Dim reader As New StreamReader(responseStream)
            Console.WriteLine(reader.ReadToEnd())
            Console.WriteLine("Download Complete, status {0}", response.StatusDescription)
            reader.Close()
            response.Close()
        End Sub
    End Class
End Namespace