using Renci.SshNet;
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text;
namespace Tiobon.Core.OPS.Tool.OPS.Tool.Helper
{
///
/// SFTP操作类
///
public class SFTPHelper
{
#region 字段或属性
private SftpClient sftp;
///
/// SFTP连接状态
///
public bool Connected { get { return sftp.IsConnected; } }
#endregion
#region 构造
///
/// 构造
///
/// IP
/// 端口
/// 用户名
/// 密码
public SFTPHelper(string ip, string port, string user, string pwd)
{
sftp = new SftpClient(ip, Int32.Parse(port), user, pwd);
}
#endregion
#region 连接SFTP
///
/// 连接SFTP
///
/// true成功
public void Connect()
{
try
{
if (!Connected)
{
sftp.Connect();
}
}
catch (Exception ex)
{
throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message));
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("连接SFTP失败,原因:{0}", ex.Message));
}
}
#endregion
#region 断开SFTP
///
/// 断开SFTP
///
public void Disconnect()
{
try
{
if (sftp != null && Connected)
{
sftp.Disconnect();
}
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("断开SFTP失败,原因:{0}", ex.Message));
throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message));
}
}
#endregion
#region SFTP上传文件
///
/// SFTP上传文件
///
/// 本地路径
/// 远程路径
public void Put(string localPath, string remotePath, string filename)
{
try
{
Connect();
using (var file = File.OpenRead(localPath))
{
if (!sftp.Exists(remotePath))
{
CreateDirectory(remotePath);
}
sftp.UploadFile(file, remotePath + "//" + filename);
Disconnect();
}
}
catch (Exception ex)
{
string log = $"文件上传失败,原因:{ex.Message}";
Const.write_log(log);
throw new Exception(log);
}
}
public void CreateDirectory(string path)
{
try
{
StringBuilder sb = new StringBuilder();
var ff = path.Split('/');
ff.ToList().ForEach(f =>
{
sb.Append($"/{f}");
if (!sftp.Exists(sb.ToString()))
{
sftp.CreateDirectory(sb.ToString());
}
});
}
catch (Exception ex)
{
string log = $"文件夹创建失败,原因:{ex.Message}";
Const.write_log(log);
throw new Exception(log);
}
}
public int Put(string localPath, string remotePath, string filename, Action action)
{
try
{
using (var file = File.OpenRead(localPath))
{
if (!sftp.Exists(remotePath))
{
StringBuilder sb = new StringBuilder();
var ff = remotePath.Split('/');
ff.ToList().ForEach(f =>
{
sb.Append($"/{f}");
if (!sftp.Exists(sb.ToString()))
{
sftp.CreateDirectory(sb.ToString());
}
});
}
sftp.UploadFile(file, remotePath + filename, action);
return 1;
}
}
catch (Exception ex)
{
string log = $"文件上传失败,原因:{ex.Message}";
MessageBoxEx.Show(log);
Const.write_log(log);
return 0;
}
}
#endregion
#region SFTP获取文件
///
/// SFTP获取文件
///
/// 远程路径
/// 本地路径
public int Get(string remotePath, string localPath)
{
try
{
Connect();
using (var file = File.OpenWrite(localPath))
{
sftp.DownloadFile(remotePath, file);
};
Disconnect();
return 1;
}
catch (Exception ex)
{
string log = $"文件下载失败,原因:{ex.Message}";
MessageBoxEx.Show(log);
Const.write_log(log);
return 0;
}
}
public int Get(string remotePath, string localPath, Action action)
{
try
{
Connect();
using (var file = File.OpenWrite(localPath))
{
sftp.DownloadFile(remotePath, file, action);
};
Disconnect();
return 1;
}
catch (Exception ex)
{
string log = $"文件下载失败,原因:{ex.Message}";
MessageBoxEx.Show(log);
Const.write_log(log);
return 0;
}
}
#endregion
#region 删除SFTP文件
///
/// 删除SFTP文件
///
/// 远程路径
public void Delete(string remoteFile)
{
try
{
Connect();
sftp.Delete(remoteFile);
Disconnect();
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
}
}
#endregion
#region 获取SFTP文件列表
///
/// 获取SFTP文件列表
///
/// 远程目录
/// 文件后缀
///
public ArrayList GetFileList(string remotePath, string fileSuffix)
{
try
{
Connect();
var files = sftp.ListDirectory(remotePath);
Disconnect();
var objList = new ArrayList();
foreach (var file in files)
{
string name = file.Name;
if (name.Length > (fileSuffix.Length + 1) && fileSuffix == name.Substring(name.Length - fileSuffix.Length))
{
objList.Add(name);
}
}
return objList;
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));
}
}
#endregion
#region 移动SFTP文件
///
/// 移动SFTP文件
///
/// 旧远程路径
/// 新远程路径
public void Move(string oldRemotePath, string newRemotePath)
{
try
{
Connect();
sftp.RenameFile(oldRemotePath, newRemotePath);
Disconnect();
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
}
}
#endregion
public bool Exists(string path)
{
Connect();
var result = sftp.Exists(path);
Disconnect();
return result;
}
}
}