Files
GateDge2023_ljy/01.扩展/HandleUtils/SFTPHelper.cs

341 lines
10 KiB
C#
Raw Normal View History

2024-04-29 17:57:07 +08:00
using Renci.SshNet;
2024-07-16 10:33:50 +08:00
using Renci.SshNet.Sftp;
2024-04-29 17:57:07 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
2024-07-16 10:33:50 +08:00
using static System.Collections.Specialized.BitVector32;
2024-04-29 17:57:07 +08:00
namespace HandleUtils
{
public class SFTPHelper
{
#region
private SftpClient _sftp;
/// <summary>
/// SFTP连接状态
/// </summary>
public bool Connected => _sftp.IsConnected;
#endregion
#region
/// <summary>
/// 构造
/// </summary>
/// <param name="ip">IP</param>
/// <param name="port">端口</param>
/// <param name="user">用户名</param>
/// <param name="pwd">密码</param>
2024-07-16 10:33:50 +08:00
public SFTPHelper(string ip, string user, string pwd, int port = 22)
2024-04-29 17:57:07 +08:00
{
2024-07-16 10:33:50 +08:00
_sftp = new SftpClient(ip, port, user, pwd);
2024-04-29 17:57:07 +08:00
Connect();
}
~SFTPHelper()
{
Disconnect();
}
#endregion
#region SFTP
/// <summary>
/// 连接SFTP
/// </summary>
/// <returns>true成功</returns>
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
/// <summary>
/// 断开SFTP
/// </summary>
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上传文件
/// <summary>
/// SFTP上传文件
/// </summary>
/// <param name="localPath">本地路径</param>
/// <param name="remotePath">远程路径</param>
2024-07-16 10:33:50 +08:00
public void Put(string localPath, string remotePath, Action<ulong> action = null)
2024-04-29 17:57:07 +08:00
{
try
{
using (var file = File.OpenRead(localPath))
{
Connect();
2024-07-16 10:33:50 +08:00
_sftp.UploadFile(file, remotePath, action);
2024-04-29 17:57:07 +08:00
}
}
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获取文件
/// <summary>
/// SFTP获取文件
/// </summary>
/// <param name="remotePath">远程路径</param>
/// <param name="localPath">本地路径</param>
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();
}
}
2024-07-16 10:33:50 +08:00
/// <summary>
/// 获取全部文件
/// </summary>
/// <param name="remotePath"></param>
/// <param name="localPath"></param>
/// <exception cref="Exception"></exception>
public void Mget(string remotePath, string localPath, Func<List<string>, bool> action = null)
{
try
{
Connect();
var files = _sftp.ListDirectory(remotePath);
var result = new List<string>();
foreach (var file in files)
{
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();
}
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="remotePath"></param>
/// <param name="localPath"></param>
/// <exception cref="Exception"></exception>
public void DownloadFile(string remotePath, string localPath, Action<ulong> 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();
}
}
2024-04-29 17:57:07 +08:00
#endregion
2024-07-16 10:33:50 +08:00
2024-04-29 17:57:07 +08:00
#region SFTP文件
/// <summary>
/// 重命名SFTP文件
/// <param name="oldFile">旧远程路径</param>
/// <param name="newFile">新远程路径</param>
/// </summary>
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文件
/// <summary>
/// 删除SFTP文件
/// </summary>
/// <param name="remoteFile">远程路径</param>
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文件列表
/// <summary>
/// 获取SFTP文件列表
/// </summary>
/// <param name="remotePath">远程目录</param>
2024-07-16 10:33:50 +08:00
/// <returns></returns>
public List<SftpFile> 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();
}
}
/// <summary>
/// 获取SFTP文件列表
/// </summary>
/// <param name="remotePath">远程目录</param>
2024-04-29 17:57:07 +08:00
/// <param name="fileSuffix">文件后缀</param>
/// <returns></returns>
public List<string> GetFileList(string remotePath, string fileSuffix)
{
try
{
//Connect();
var files = _sftp.ListDirectory(remotePath);
var objList = new List<string>();
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文件
/// <summary>
/// 移动SFTP文件
/// </summary>
/// <param name="oldRemotePath">旧远程路径</param>
/// <param name="newRemotePath">新远程路径</param>
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
}
}