Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;
Using System. Net;
Using System. Windows. Forms;
Using System. Globalization;
Namespace FtpTest1
{
Public class FtpWeb
{
String ftpServerIP;
String ftpRemotePath;
String ftpUserID;
String ftpPassword;
String ftpURI;
/// <Summary>
/// Connect to FTP
/// </Summary>
/// <Param name = "FtpServerIP"> FTP connection address </param>
/// <Param name = "FtpRemotePath"> specifies the current directory after successful FTP connection. If this parameter is not specified, it is the root directory by default. </param>
/// <Param name = "FtpUserID"> User name </param>
/// <Param name = "FtpPassword"> password </param>
Public FtpWeb (string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
{
FtpServerIP = FtpServerIP;
FtpRemotePath = FtpRemotePath;
FtpUserID = FtpUserID;
FtpPassword = FtpPassword;
FtpURI = "ftp: //" + ftpServerIP + "/" + ftpRemotePath + "/";
}
/// <Summary>
/// Upload
/// </Summary>
/// <Param name = "filename"> </param>
Public void Upload (string filename)
{
FileInfo fileInf = new FileInfo (filename );
String uri = ftpURI + fileInf. Name;
FtpWebRequest reqFTP;
ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri (uri ));
ReqFTP. Credentials = new NetworkCredential (ftpUserID, ftpPassword );
ReqFTP. KeepAlive = false;
ReqFTP. Method = WebRequestMethods. Ftp. UploadFile;
ReqFTP. UseBinary = true;
ReqFTP. ContentLength = fileInf. Length;
Int buffLength = 2048;
Byte [] buff = new byte [buffLength];
Int contentLen;
FileStream fs = fileInf. OpenRead ();
Try
{
Stream strm = reqFTP. GetRequestStream ();
ContentLen = fs. Read (buff, 0, buffLength );
While (contentLen! = 0)
{
Strm. Write (buff, 0, contentLen );
ContentLen = fs. Read (buff, 0, buffLength );
}
Strm. Close ();
Fs. Close ();
}
Catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert ("FtpWeb", "Upload Error -->" + ex. Message );
}
}
/// <Summary>
/// Download
/// </Summary>
/// <Param name = "filePath"> </param>
/// <Param name = "fileName"> </param>
Public void Download (string filePath, string fileName)
{
FtpWebRequest reqFTP;
Try
{
FileStream outputStream = new FileStream (filePath + "\" + fileName, FileMode. Create );
ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri (ftpURI + fileName ));
ReqFTP. Method = WebRequestMethods. Ftp. DownloadFile;
ReqFTP. UseBinary = true;
ReqFTP. Credentials = new NetworkCredential (ftpUserID, ftpPassword );
FtpWebResponse response = (FtpWebResponse) reqFTP. GetResponse ();
Stream ftpStream = response. GetResponseStream ();