There are many situation in which you would want to connect to an FTP server and perform downloading of FTP Files. here is a helper class to perorm that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace Yournamespace.Core.FTP
{
public class FTPManager
{
private string _FTPHost;
private string _FTPUser;
private string _FTPPassword;
public string FTPHost
{
get { return _FTPHost; }
set { _FTPHost = value; }
}
public string FTPUser
{
get { return _FTPUser; }
set { _FTPUser = value; }
}
public string FTPPassword
{
get { return _FTPPassword; }
set { _FTPPassword = value; }
}
public void Upload(string filename)
{
}
public void Download(string filename)
{
FileStream outputStream = new FileStream(filename, FileMode.Create);
FtpWebRequest request = null;
FtpWebResponse response = null;
Stream ftpStream = null;
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new
Uri("ftp://" + this.FTPHost + "/" + filename));
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.UseBinary = true;
request.Credentials = new NetworkCredential(this.FTPUser, this.FTPPassword);
response = (FtpWebResponse)request.GetResponse();
ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
}
catch (Exception e)
{
throw e;
}
finally
{
ftpStream.Close();
outputStream.Close();
response.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace Yournamespace.Core.FTP
{
public class FTPManager
{
private string _FTPHost;
private string _FTPUser;
private string _FTPPassword;
public string FTPHost
{
get { return _FTPHost; }
set { _FTPHost = value; }
}
public string FTPUser
{
get { return _FTPUser; }
set { _FTPUser = value; }
}
public string FTPPassword
{
get { return _FTPPassword; }
set { _FTPPassword = value; }
}
public void Upload(string filename)
{
}
public void Download(string filename)
{
FileStream outputStream = new FileStream(filename, FileMode.Create);
FtpWebRequest request = null;
FtpWebResponse response = null;
Stream ftpStream = null;
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new
Uri("ftp://" + this.FTPHost + "/" + filename));
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.UseBinary = true;
request.Credentials = new NetworkCredential(this.FTPUser, this.FTPPassword);
response = (FtpWebResponse)request.GetResponse();
ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
}
catch (Exception e)
{
throw e;
}
finally
{
ftpStream.Close();
outputStream.Close();
response.Close();
}
}
}
}
Hi,Designing Easy-to-use Websites provides practical guidelines with Web Design Cochin on how to design usable e-business websites; websites that not only contain up-to-date, effective information that is easy to find, but sites that actually make it easy for users to do all the tasks that they have come to the site to achieve.Thanks...........
ReplyDelete