Download files from Server to local pc using FTP

I just created simple FTP program which will download specified files from server to local pc using FTP. Before running this application you have to specify the IP address of the server and which file you want to download from the server and local filepath. before running this application you have to check on server that FTP service is installed or not, if not then you have configure that to allow FTP.

Program.cs file


namespace FTPPoc
{
class Program
{
public static Settings1 FTPPocSettings = null;
static void Main(string[] args)
{
FTPPocSettings = new Settings1();
//Console.WriteLine(FTPPocSettings.RemoteFileName);
//Console.ReadLine();
Download();
//Console.ReadLine();
}

public static Uri getServerUri()
{
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = Uri.UriSchemeFtp;
uriBuilder.Host = FTPPocSettings.Host;
uriBuilder.Path = "";
Uri serverUri = new Uri(uriBuilder.ToString());
return serverUri;
}

public static void Download()
{
// We start to create the FTPWebRequest ftp_server_url must contain
// the path of the file that we want to download. Ex : ftp://myftp/file_to_download.txt
FtpWebRequest reqObj = (FtpWebRequest) FtpWebRequest.Create(getServerUri() + FTPPocSettings.RemoteFileName);

// Set the credentials
reqObj.Credentials = new NetworkCredential(FTPPocSettings.UserName, FTPPocSettings.Password);

// Turn off KeepAlive (will close connection on completion)
reqObj.KeepAlive = false;

// We want a binary
reqObj.UseBinary = true;

// Define the action required (in this case, download a file).
reqObj.Method = WebRequestMethods.Ftp.DownloadFile;

// If we were using a method that uploads data e.g. UploadFile
// we would open the ftp.GetRequestStream here an send the data.

// Get the response to the Ftp request and the associated stream.
using( FtpWebResponse resObj = (FtpWebResponse) reqObj.GetResponse() )
{
// Stream of the ftp response, we get the ftp stream
using( Stream responseStream = resObj.GetResponseStream() )
{
//Stream responseStream = resObj.GetResponseStream();
// Stream of the file we are going to create,
// We create the destination file and attach it to the FileStream.
using( FileStream fs = File.Create(FTPPocSettings.LocalFilePath + FTPPocSettings.LocalFileName) )
{
try
{
// Copy the file contents to the request stream.
byte[] buffer = new byte[2048];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
}
while( read != 0 );
// IMPORTANT: Close the response stream before sending the request.
responseStream.Close();
fs.Flush();
fs.Close();
}
catch( Exception ex )
{
Console.WriteLine(ex.Message);
fs.Close();
}
}
responseStream.Close();
}
resObj.Close();
}
}
}
}

create settings file
and add the following names in that with values.

name="UserName" --- Value=""
name="Password" --- Value=""
name="Host" -- Value = 192.168.1.5
name="RemoteFileName" --- Value="DBBackup01Apr2008.zip"
name="LocalFileName" --- Value= "DBBackup01Apr2008.zip"
name="LocalFilePath" --- Value= E:\FTPPoc\FTPPoc\DownloadedFiles\


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: