1. FTP.cs
using System;
using System.IO;
using System.Net;
namespace FTP_DLL
{
public class FTP
{
private string _remoteHost, _remoteUser, _remotePass, _remotePort;
public FTP(String remoteHost, String remotePort, String remoteUser, String remotePass)
{
_remoteHost = remoteHost;
_remoteUser = remoteUser;
_remotePass = remotePass;
_remotePort = remotePort;
}
public void SSLUpload(String filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = String.Format("ftp://{0}:{1}/{2}", _remoteHost, _remotePort, fileInf.Name);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.EnableSsl = true;
reqFTP.Credentials = new NetworkCredential(_remoteUser, _remotePass);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
//Method1:Bypass Cert
ServicePointManager.ServerCertificateValidationCallback = (s, certificate, chain, sslPolicyErrors) => true;
//Method2:Accept Cert
//X509Certificate cert = new X509Certificate2("C:\\Path\\cert.pfx", "password");
//string b = cert.GetCertHashString();
//Console.WriteLine("hash string " + b);
//ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
//{
//if (certificate.GetCertHashString().Equals(b))
//{
//Console.WriteLine("ServerCertificateValidation true");
//return true;
//}
//else
//{
//Console.WriteLine("ServerCertificateValidation false");
//return false;
//}
//};
//reqFTP.ContentLength = fileInf.Length;
//reqFTP.ContentLength = 10000;
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)
{
Console.WriteLine(ex.Message, "Upload err");
}
}
public void SSLDownload(String filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = String.Format("ftp://{0}:{1}/{2}", _remoteHost, _remotePort, fileInf.Name);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.EnableSsl = true;
reqFTP.Credentials = new NetworkCredential(_remoteUser, _remotePass);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
//Method1:Bypass Cert
ServicePointManager.ServerCertificateValidationCallback = (s, certificate, chain, sslPolicyErrors) => true;
//Method2:Accept Cert
//X509Certificate cert = new X509Certificate2("C:\\Path\\cert.pfx", "password");
//string b = cert.GetCertHashString();
//Console.WriteLine("hash string " + b);
//ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
//{
//if (certificate.GetCertHashString().Equals(b))
//{
//Console.WriteLine("ServerCertificateValidation true");
//return true;
//}
//else
//{
//Console.WriteLine("ServerCertificateValidation false");
//return false;
//}
//};
using (Stream ftpStream = reqFTP.GetResponse().GetResponseStream())
{
using (Stream fileStream = File.Create(filename))
{
byte[] buffer = new byte[10240];
int read;
while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
}
}
}
}
}
}
2. App.Config
Add Setting And Value in XML format.
1. setting name="TargetFtpSource" serializeas="String" value>X.X.X.X
2. setting name="TargetFtpSourcePort" serializeas="String" value>XX
3. setting name="TargetFtpSecureUser" serializeas="String" value>XXXX
4. setting name="TargetFtpSecurePass" serializeas="String" value>XXXX
3. Program.cs
using System;
using FTP_DLL.Properties;
namespace FTP_DLL
{
class Program
{
static void Main(string[] args)
{
try
{
FTP ftp = new FTP(Settings.Default.TargetFtpSource, Settings.Default.TargetFtpSourcePort, Settings.Default.TargetFtpSecureUser, Settings.Default.TargetFtpSecurePass);
//Upload and Download Function (Type File Path e.g.:C:\XXX)
ftp.SSLUpload("FILE_PATH");
ftp.SSLDownload("FILE_PATH");
}
catch (Exception e)
{
Console.WriteLine("Caught Error :" + e.Message);
}
}
}
}