C#实现SSH、SCP、FTP等操作
C#没有自带的SSH、SCP、FTP等操作的方法库,自己编写又太麻烦,这里将使用第三方Renci.SshNet.dll动态链接库实现这些操作
一、获取RENCI.SSHNET.DLL
二、将RENCI.SSHNET.DLL添加进C#工程
我使用visual studio 2015作为IDE,将Renci.SshNet.dll添加进C#工程并调用的方法如下:
1. 添加引用
如上图所示,右键解决方案下项目中的引用,选择添加引用,在弹窗中选择浏览按钮,找到Renci.SshNet.dll动态链接库,点击添加得到如下图所示
勾选你添加的Renci.SshNet.dll,点击确定
2. 调用
在添加了引用后直接使用下面的语句即可调用Renci.SshNet.dll动态链接库
using Renci.SshNet;
- 1
三、使用RENCI.SSHNET.DLL
下面以SCP为例,FTP的二次封装可以参考开头给出的博文
SshNet动态链接库已经将FTP的基本功能封装成一个SCP类,在官方的说明文档中的命名空间目录下可以找到SCP类的说明,包括方法、属性、事件等。在使用中,我根据需要对这些方法、属性等进行了二次封装。
public class SFTPHelper
{
#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 SFTPHelper(string ip, string port, string user, string pwd)
{
sftp = new SftpClient(ip, Int32.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)
{
//return false;
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("连接SFTP失败,原因:{0}", ex.Message));
throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message));
//连接SFTP失败,原因:An established connection was aborted by the software in your host machine.
}
}
#endregion
#region 断开SFTP
/// <summary>
/// 断开SFTP
/// </summary>
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上传文件
/// <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);
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获取文件
/// <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)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
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)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件删除失败,原因:{0}", ex.Message));
}
}
#endregion
private int day(string name)
{
if (!name.Contains("_"))
return 0;
name = name.Split('_')[1];
if (name.Length < 8)
return 0;
name = name.Substring(0, 8);
DateTime start = DateTime.ParseExact(name, "yyyyMMdd", null);
DateTime end = Convert.ToDateTime(DateTime.Now.ToShortDateString());
TimeSpan sp = end.Subtract(start);
return sp.Days;
}
#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)
{
/*DateTime start = Convert.ToDateTime(file.LastWriteTime.ToShortDateString());
DateTime end = Convert.ToDateTime(DateTime.Now.ToShortDateString());
TimeSpan sp = end.Subtract(start);*/
string name = file.Name;
if (day(name) != 1)
continue;
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文件
/// <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)
{
// TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", ex.Message));
}
}
#endregion
public static int DownloadFtp(string filePath, string localPath, string fileName, string ftpServerIP, string ftpPort, string ftpUserID, string ftpPassword)
{
string localFileName = localPath + "/" + fileName;
if (!Directory.Exists(localPath))
{
Directory.CreateDirectory(localPath);
}
string remoteFileName = filePath+"/"+fileName;
try
{
if (File.Exists(localFileName))
{
File.Delete(localFileName);
}
using (var sftp = new SftpClient(ftpServerIP, Convert.ToInt32(ftpPort), ftpUserID, ftpPassword))
{
sftp.Connect();
using (var file = File.OpenWrite(localFileName))
{
sftp.DownloadFile(remoteFileName, file);
}
sftp.Disconnect();
//Log.getInstace().WriteSysInfo("下载文件{localFileName}成功", "info");
Console.WriteLine("下载文件成功,文件路径: "+ localFileName);
return 0;
}
}
catch (Exception e)
{
throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", e.Message));
return -2;
}
}
}
}