FileInfo file = new FileInfo(DatFileNameAll);
SeagateFtp.Upload(file,Path.Combine(AppConfig.seagateFtpPath, DatFileName));
log.WriteLog("Seagate ftp send Success");
public class FtpHelper
{
private static readonly string rootPath = "/";
private static readonly int defaultReadWriteTimeout = 20000;
private static readonly int defaultFtpPort = 21;
public FtpHelper(string[] ftp )
: this(ftp[0], ftp[1], ftp[2], defaultFtpPort, null, false, true, true, defaultReadWriteTimeout)
{
}
public FtpHelper(string host, string username, string password, int port, IWebProxy proxy, bool enableSsl,
bool useBinary, bool usePassive, int readWriteTimeout)
{
this.Host = host.ToUpper().StartsWith("ftp://") ? host : "ftp://" + host;
this.Username = username;
this.Password = password;
this.Port = port;
this.Proxy = proxy;
this.EnableSsl = enableSsl;
this.UseBinary = useBinary;
this.UsePassive = usePassive;
this.ReadWriteTimeout = readWriteTimeout;
}
private string host = string.Empty;
private string username = string.Empty;
private string password = string.Empty;
private IWebProxy proxy = null;
private bool useBinary = true;
private bool usePassive = true;
private bool enableSsl = true;
private string method = string.Empty;
private int port = defaultFtpPort;
private int readWriteTimeout = defaultReadWriteTimeout;
private string remotePath = rootPath;
public string Username
{
get {
return username; }
set {
username = value; }
}
public string Password
{
get {
return password; }
set {
password = value; }
}
public IWebProxy Proxy
{
get {
return proxy; }
set {
proxy = value; }
}
public bool UseBinary
{
get {
return useBinary; }
set {
useBinary = value; }
}
public bool UsePassive
{
get {
return usePassive; }
set {
usePassive = value; }
}
public bool EnableSsl
{
get {
return enableSsl; }
set {
enableSsl = value; }
}
public string Host
{
get {
return host; }
set {
host = value; }
}
public int Port
{
get {
return port; }
set {
port = value; }
}
public int ReadWriteTimeout
{
get {
return readWriteTimeout; }
set {
readWriteTimeout = value; }
}
public string RemotePath
{
get
{
return remotePath;
}
set
{
string result = rootPath;
if (!string.IsNullOrEmpty(value) && value != rootPath)
result = Path.Combine(Path.Combine(rootPath, value.TrimStart('/').TrimEnd('/')), "/");
this.remotePath = result;
}
}
private FtpWebRequest CreateConnection(string url, string method)
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
request.Credentials = new NetworkCredential(this.Username, this.Password);
request.Proxy = Proxy;
request.KeepAlive = false;
request.UseBinary = UseBinary;
request.UsePassive = UsePassive;
request.EnableSsl = EnableSsl;
request.Method = method;
Console.WriteLine(request);
return request;
}
private string UrlCombine(string host, string remotePath, string fileName)
{
string result = new Uri(new Uri(new Uri(host.TrimEnd('/')), remotePath), fileName).ToString();
return result;
}
public void Rename(string path, string currentFileName, string newFileName)
{
string url = Host + "/" + path + "/" + currentFileName;
FtpWebRequest request = CreateConnection(url, WebRequestMethods.Ftp.Rename);
request.RenameTo = newFileName;
WebResponse Response = (FtpWebResponse)request.GetResponse();
}
public bool Upload(FileInfo localFile, string remoteFileName)
{
bool result = false;
if (localFile.Exists)
{
string url = UrlCombine(Host, RemotePath, remoteFileName);
FtpWebRequest request = CreateConnection(url, WebRequestMethods.Ftp.UploadFile);
using (Stream rs = request.GetRequestStream())
using (FileStream fs = localFile.OpenRead())
{
byte[] buffer = new byte[1024 * 4];
int count = fs.Read(buffer, 0, buffer.Length);
while (count > 0)
{
rs.Write(buffer, 0, count);
count = fs.Read(buffer, 0, buffer.Length);
}
fs.Close();
result = true;
}
return result;
}
return false;
}
public bool Download(string serverName, string localName)
{
bool result = false;
using (FileStream fs = new FileStream(localName, FileMode.OpenOrCreate))
{
string url = UrlCombine(Host, RemotePath, serverName);
Console.WriteLine(url);
FtpWebRequest request = CreateConnection(url, WebRequestMethods.Ftp.DownloadFile);
request.ContentOffset = fs.Length;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
fs.Position = fs.Length;
byte[] butter = new byte[1024 * 4];
int count = response.GetResponseStream().Read(butter, 0, butter.Length);
while (count > 0)
{
fs.Write(butter, 0, count);
count = response.GetResponseStream().Read(butter, 0, butter.Length);
}
response.GetResponseStream().Close();
}
result = true;
}
return result;
}