Pages

Monday, August 6, 2012

File Download, Upload, Delete in FTP Location using C#


If you have difficulties with FTP File operations, you are at the right place. I am going to present some self explanatory code snippets in C# to accomplish FTP FILE operations like downloading, uploading and deleting.

using System.Net;

First of all, this directive is mandatory for FTP Operations.
File Download From FTP:

string localPath = @"G:\FTPTrialLocalPath\";
string fileName = "arahimkhan.txt";

FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source/" + fileName);
requestFileDownload.Credentials = new NetworkCredential("khanrahim", "arkhan22");
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;

FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();

Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create);  

int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);               

while (bytesRead > 0)
{
  writeStream.Write(buffer, 0, bytesRead);
  bytesRead = responseStream.Read(buffer, 0, Length);
}   

responseStream.Close();             
writeStream.Close();

requestFileDownload = null;
responseFileDownload = null;

File Upload to FTP:

string localPath = @"G:\FTPTrialLocalPath\";
string fileName = "arahimkhan.txt";

FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1/Destination/" + fileName);
requestFTPUploader.Credentials = new NetworkCredential("khanrahim", "arkhan22");
requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;

FileInfo fileInfo = new FileInfo(localPath + fileName);
FileStream fileStream = fileInfo.OpenRead();

int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];

Stream uploadStream = requestFTPUploader.GetRequestStream();
int contentLength = fileStream.Read(buffer, 0, bufferLength);

while (contentLength != 0)
{
 uploadStream.Write(buffer, 0, contentLength);
 contentLength = fileStream.Read(buffer, 0, bufferLength);
}
                    
uploadStream.Close();
fileStream.Close();

requestFTPUploader = null;

File Delete From FTP:

string fileName = "arahimkhan.txt";

FtpWebRequest requestFileDelete = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source/" + fileName);
requestFileDelete.Credentials = new NetworkCredential("khanrahim", "arkhan22");
requestFileDelete.Method = WebRequestMethods.Ftp.DeleteFile;

FtpWebResponse responseFileDelete = (FtpWebResponse)requestFileDelete.GetResponse();

Retrieve File List from FTP Directory:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source");
request.Credentials = new NetworkCredential("khanrahim", "arkhan22");
request.Method = WebRequestMethods.Ftp.ListDirectory;

StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());

string fileName = streamReader.ReadLine();

while (fileName != null)
{
 Console.Writeline(fileName );
 fileName = streamReader.ReadLine();
}

request = null;
streamReader = null;

using System.IO;

You have to use this directive for Local File Operation.
Retrieve File List from Local Directory:

string localPath = @"G:\FTPTrialLocalPath\";

string[] files = Directory.GetFiles(localPath);

foreach (string filepath in files)
{
  string fileName = Path.GetFileName(filepath);
  Console.WriteLine(fileName);
}
You can download source code.

No comments:

Post a Comment