Monday, April 2, 2012

how to Download, Upload, Delete files in FTP and local drive using C#

using System.Net;

call this in your .cs file.


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;
Retrieve File List from Local Directory: (using System.IO;)

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

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

foreach (string filepath in files)
{
  string fileName = Path.GetFileName(filepath);
  Console.WriteLine(fileName);
}
Delete A File from Local Directory:

  System.IO.File.Delete("FilePath");
inspiration by rahim's blog....
http://khanrahim.wordpress.com/2010/09/03/file-download-upload-delete-in-ftp-location-using-c/

No comments:

Post a Comment