网上的一些SFTP的代码大多有着一些问题,在反复搜索往后终于找到了一个行的通的,在此记录一下。
要在C#实现SFTP操作,首先必须要下载Renci.SshNet这个第三方类库,下载地址为:https://github.com/sshnet/SSH.NET 下载运行后天生dll引入到项目中便能利用,代码如下:
/// <summary>

/// FTP类
/// </summary>
public class SFTP
{
public class SFTPOperation
{
#region 字段或属性
private SftpClient sftp;
/// <summary>
/// SFTP连接状态
/// </summary>
public bool Connected { get { return sftp.IsConnected; } }
#endregion
#region 布局
/// <summary>
/// 布局
/// </summary>
/// <param name=\公众ip\公众>IP</param>
/// <param name=\"大众port\"大众>端口</param>
/// <param name=\"大众user\公众>用户名</param>
/// <param name=\"大众pwd\公众>密码</param>
public SFTPOperation(string ip, string port, string user, string pwd)
{
sftp = new SftpClient(ip, Int32.Parse(port), user, pwd);
}
#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>
/// <param name=\公众fileName\公众>文件名</param>
public int Put(string localPath, string remotePath,string fileName)
{
try
{
using (var file = File.OpenRead(localPath))
{
Connect();
bool b = sftp.Exists(remotePath);//判断路径是否存在
if (!b)
{
sftp.CreateDirectory(remotePath);//不存在则先创建
}
sftp.UploadFile(file, remotePath+ fileName);
Disconnect();
}
return 1;
}
catch (Exception ex)
{
//throw new Exception(string.Format(\"大众SFTP文件上传失落败,缘故原由:{0}\"大众, ex.Message));
return 0;
}
}
#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);
Disconnect();
File.WriteAllBytes(localPath, byt);
}
catch (Exception ex)
{
throw new Exception(string.Format(\"大众SFTP文件获取失落败,缘故原由:{0}\"大众, ex.Message));
}
}
#endregion
#region 删除SFTP文件
/// <summary>
/// 删除SFTP文件
/// </summary>
/// <param name=\"大众remoteFile\公众>远程路径</param>
public void Delete(string remoteFile)
{
try
{
Connect();
sftp.Delete(remoteFile);
Disconnect();
}
catch (Exception ex)
{
throw new Exception(string.Format(\"大众SFTP文件删除失落败,缘故原由:{0}\"大众, ex.Message));
}
}
#endregion
#region 获取SFTP文件列表
/// <summary>
/// 获取SFTP文件列表
/// </summary>
/// <param name=\"大众remotePath\公众>远程目录</param>
/// <param name=\"大众fileSuffix\"大众>文件后缀</param>
/// <returns></returns>
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)
{
throw new Exception(string.Format(\公众SFTP文件列表获取失落败,缘故原由:{0}\公众, ex.Message));
}
}
#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);
Disconnect();
}
catch (Exception ex)
{
throw new Exception(string.Format(\"大众SFTP文件移动失落败,缘故原由:{0}\"大众, ex.Message));
}
}
#endregion
}
}