using Renci.SshNet;
using Renci.SshNet.Sftp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using static System.Collections.Specialized.BitVector32;
namespace HandleUtils
{
public class SFTPHelper
{
#region 字段或属性
private SftpClient _sftp;
///
/// SFTP连接状态
///
public bool Connected => _sftp.IsConnected;
#endregion
#region 构造
///
/// 构造
///
/// IP
/// 端口
/// 用户名
/// 密码
public SFTPHelper(string ip, string user, string pwd, int port = 22)
{
_sftp = new SftpClient(ip, port, user, pwd);
Connect();
}
~SFTPHelper()
{
Disconnect();
}
#endregion
#region 连接SFTP
///
/// 连接SFTP
///
/// true成功
public bool Connect()
{
try
{
if (!Connected)
{
_sftp.Connect();
}
return true;
}
catch (Exception ex)
{
throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message));
}
}
#endregion
#region 断开SFTP
///
/// 断开SFTP
///
public void Disconnect()
{
try
{
if (_sftp != null && Connected)
{
_sftp.Disconnect();
}
}
catch (Exception ex)
{
throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message));
}
}
#endregion
#region SFTP上传文件
///
/// SFTP上传文件
///
/// 本地路径
/// 远程路径
public void Put(string localPath, string remotePath, string msg = "", Action action = null)
{
try
{
using (var file = File.OpenRead(localPath))
{
Connect();
_sftp.UploadFile(file, remotePath, action);
msg = "上传成功!";
}
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件上传失败,原因:{0}", ex.Message));
//throw new Exception(string.Format("SFTP文件上传失败,原因:{0}", ex.Message));
msg = ex.Message;
}
finally
{
Disconnect();
}
}
#endregion
#region SFTP获取文件
///
/// SFTP获取文件
///
/// 远程路径
/// 本地路径
public void Get(string remotePath, string localPath)
{
try
{
Connect();
var byt = _sftp.ReadAllBytes(remotePath);
File.WriteAllBytes(localPath, byt);
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
}
finally
{
Disconnect();
}
}
///
/// 获取全部文件
///
///
///
///
public void Mget(string remotePath, string localPath, Func, bool> action = null)
{
try
{
Connect();
var files = _sftp.ListDirectory(remotePath);
var result = new List();
foreach (var file in files)
{
if (file.Name.Contains("%LEXMK_SFTP_PRD%HUIWE_SFTP_PRD%BINARY%"))
continue;
using (var fs = File.OpenWrite(localPath + file.Name))
{
result.Add(file.Name);
_sftp.DownloadFile(file.FullName, fs);
}
}
action?.Invoke(result);
}
catch (Exception ex)
{
throw new Exception(string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
}
finally
{
Disconnect();
}
}
///
/// 下载文件
///
///
///
///
public void DownloadFile(string remotePath, string localPath, Action action = null)
{
try
{
Connect();
using (var fs = File.OpenWrite(localPath))
{
_sftp.DownloadFile(remotePath, fs, action);
}
}
catch (Exception ex)
{
throw new Exception(string.Format("SFTP下载文件失败,原因:{0}", ex.Message));
}
finally
{
Disconnect();
}
}
#endregion
#region 重命名SFTP文件
///
/// 重命名SFTP文件
/// 旧远程路径
/// 新远程路径
///
public void Rename_SFTP(string oldFilePath, string newFilePath)
{
try
{
Connect();
_sftp.RenameFile(oldFilePath, newFilePath);
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
}
finally
{
Disconnect();
}
}
#endregion
#region 删除SFTP文件
///
/// 删除SFTP文件
///
/// 远程路径
public void Delete(string remoteFile)
{
try
{
Connect();
_sftp.Delete(remoteFile);
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
}
finally
{
Disconnect();
}
}
#endregion
#region 获取SFTP文件列表
///
/// 获取SFTP文件列表
///
/// 远程目录
///
public List GetFileList(string remotePath)
{
try
{
var files = _sftp.ListDirectory(remotePath);
return files.ToList();
}
catch (Exception ex)
{
throw new Exception(string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));
}
finally
{
Disconnect();
}
}
///
/// 获取SFTP文件列表
///
/// 远程目录
/// 文件后缀
///
public List GetFileList(string remotePath, string fileSuffix)
{
try
{
//Connect();
var files = _sftp.ListDirectory(remotePath);
var objList = new List();
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));
}
finally
{
Disconnect();
}
}
#endregion
#region 移动SFTP文件
///
/// 移动SFTP文件
///
/// 旧远程路径
/// 新远程路径
public void Move(string oldRemotePath, string newRemotePath)
{
try
{
Connect();
_sftp.RenameFile(oldRemotePath, newRemotePath);
}
catch (Exception ex)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
}
finally
{
Disconnect();
}
}
#endregion
}
}