1
This commit is contained in:
@@ -5,7 +5,7 @@ using System.Text;
|
||||
|
||||
namespace HandleUtils
|
||||
{
|
||||
public class Base64Helper
|
||||
public static class Base64Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// Base64加密,采用utf8编码方式加密
|
||||
|
||||
@@ -5,6 +5,9 @@ using System.Text;
|
||||
|
||||
namespace HandleUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// 加密/解密
|
||||
/// </summary>
|
||||
public static class EncryptHelper
|
||||
{
|
||||
public static string UrlEncode(string value)
|
||||
|
||||
@@ -31,6 +31,12 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Renci.SshNet">
|
||||
<HintPath>..\..\packages\SSH.NET.2020.0.2\lib\net40\Renci.SshNet.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Renci.SshNet.Async">
|
||||
<HintPath>..\..\packages\Renci.SshNet.Async.1.4.0\lib\net40\Renci.SshNet.Async.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
@@ -42,6 +48,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Base64Helper.cs" />
|
||||
<Compile Include="EncryptHelper.cs" />
|
||||
<Compile Include="SFTPHelper.cs" />
|
||||
<Compile Include="WebHelper.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
253
01.扩展/HandleUtils/SFTPHelper.cs
Normal file
253
01.扩展/HandleUtils/SFTPHelper.cs
Normal file
@@ -0,0 +1,253 @@
|
||||
using Renci.SshNet;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
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>
|
||||
public SFTPHelper(string ip, string user, string pwd, string port = "22")
|
||||
{
|
||||
_sftp = new SftpClient(ip, int.Parse(port), user, pwd);
|
||||
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>
|
||||
public void Put(string localPath, string remotePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var file = File.OpenRead(localPath))
|
||||
{
|
||||
Connect();
|
||||
_sftp.UploadFile(file, remotePath);
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#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>
|
||||
/// <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
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user