You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

60 lines
1.3 KiB

using System;
using System.IO;
using System.Net;
namespace EC.Utils
{
/// <summary>
/// FTPHelper
/// 版 本:V3.0.0
/// 版 权:EasyCode
/// 作 者:LXC
/// </summary>
public class FTPHelper
{
private FtpWebRequest _ftp;
private readonly string _ftpUrl;
private readonly string _userName;
private readonly string _passWord;
public FTPHelper(string ftpUrl, string userName, string passWord)
{
this._ftpUrl = ftpUrl;
this._userName = userName;
this._passWord = passWord;
Connect();
}
private void Connect()
{
_ftp = (FtpWebRequest)WebRequest.Create(new Uri(this._ftpUrl));
_ftp.UseBinary = true;
_ftp.Credentials = new NetworkCredential(this._userName, this._passWord);
}
public bool UpLoad(string file)
{
FileInfo copyfile = new FileInfo(file);
_ftp.Method = WebRequestMethods.Ftp.UploadFile;
const int bufflength = 2048;
byte[] buff = new byte[bufflength];
FileStream upstream = copyfile.OpenRead();
try
{
Stream ftpstream = _ftp.GetRequestStream();
int readLength;
while ((readLength = upstream.Read(buff, 0, bufflength)) > 0)
{
ftpstream.Write(buff, 0, readLength);
}
upstream.Close();
ftpstream.Close();
return true;
}
catch
{
return false;
}
}
}
}