Files

98 lines
3.3 KiB
C#

using FluentFTP;
namespace Cowain.Bake.Communication.Sokects
{
//全部都是远程操作
public class FtpHelper
{
#region
public string IpAddr { get; set; } /// IP地址
public string RelatePath { get; set; } /// 相对路径
public int Port { get; set; } /// 端口号
public string UserName { get; set; } /// 用户名
public string Password { get; set; } /// 密码
//https://blog.csdn.net/fengershishe/article/details/129140618
public FtpHelper()
{
}
public FtpHelper(string ipAddr, int port, string userName, string password, string relatePath = "")
{
this.IpAddr = ipAddr;
this.Port = port;
this.UserName = userName;
this.Password = password;
this.RelatePath = relatePath;
}
#endregion
#region
public FtpListItem[] ListDir()
{
FtpListItem[] lists;
using (var ftpClient = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
{
ftpClient.Connect(); // 连接到FTP服务器
ftpClient.SetWorkingDirectory(this.RelatePath);
lists = ftpClient.GetListing();
}
return lists;
}
// 上传文件
public bool UpLoad(string src, string desc)
{
using (var ftpClient = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
{
ftpClient.Connect();
FtpStatus result = ftpClient.UploadFile(src, desc); // ("local/file.txt", "remote/file.txt");
ftpClient.Disconnect();
return FtpStatus.Success == result ? true : false;
}
}
///创建目录(远程创建)
private bool CheckDirIsExists(string dir)
{
bool flag = false;
using (var ftpClient = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
{
ftpClient.Connect();
ftpClient.SetWorkingDirectory(this.RelatePath);
flag = ftpClient.DirectoryExists(dir);
if (!flag)
{
flag = ftpClient.CreateDirectory(dir); //client.CreateDirectory("remote/directory");
}
}
return flag;
}
public void DeleteFile(string dir)
{
using (var ftpClient = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
{
ftpClient.Connect();
ftpClient.SetWorkingDirectory(this.RelatePath);
ftpClient.DeleteFile(dir);
ftpClient.Disconnect();
}
}
public bool DownloadFile(string localAddress, string remoteAddress)
{
using (var ftpClient = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
{
ftpClient.SetWorkingDirectory(this.RelatePath);
ftpClient.Connect();
if (ftpClient.DownloadFile(localAddress, remoteAddress) == FtpStatus.Success)
{
return true;
}
}
return false;
}
#endregion
}
}