Home     ProductivityTools     Articles     Fun fatcs     How To     C Sharp     Science News     Contact Us     Submit your website     General Blog     Jobs      
c# Keywords
ASP .NET
C# Utopia
How do I add text items t
How to download a file fr
How to print all values i
How to copy text to buffe
How to bind dataset to da
How to parse xml using li
How to launch Bat file
How to launch message box
How to read text file
How to append to text fil
How to set focus on a con
How to generate new GUID
How to download a file from ftp server?

 

public void Download(string filePath, string fileName)

{

FtpWebRequest reqFTP;

try

{

//filePath: The full path where the file is to be created.

//fileName: Name of the file to be createdNeed not name on

// the FTP server. name name()

FileStream outputStream = new FileStream(filePath + "\\" +

fileName, FileMode.Create);

string ftpServerIP = this.textBoxFtpServerIP.Text + this.textBoxForFilePath.Text;

string ftpUserID = this.textBoxForUserID.Text;

string ftpPassword = this.textBoxForPassword.Text;

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"

+ ftpServerIP + "/" + fileName));

reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

reqFTP.UseBinary = true;

reqFTP.Credentials = new NetworkCredential(ftpUserID,

ftpPassword);

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

Stream 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);

}

ftpStream.Close();

outputStream.Close();

response.Close();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}