| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 | 
							- 
 
- using IMCS.Device.body;
 
- using Renci.SshNet;
 
- using System.IO;
 
- namespace System.Text
 
- {
 
-     public class DeviceSFTP
 
-     {
 
-         #region 声明一个ScpClient类型的变量
 
-         private ScpClient ssh;
 
-         private SftpClient sftp;
 
-         private SshClient sshClient;
 
-  
 
-        
 
-         /// <summary>
 
-         /// SCP连接状态
 
-         /// </summary>
 
-         public bool Connected { get { return ssh.IsConnected; } }
 
-         #endregion
 
-         #region
 
-         /// <summary>
 
-         /// 构造方法
 
-         /// </summary>
 
-         /// <param name="ip">IP</param>
 
-         /// <param name="port">端口</param>
 
-         /// <param name="user">用户名</param> 
 
-         /// <param name="pwd">密码</param>
 
-         public DeviceSFTP(string ip, string port, string user, string pwd)
 
-         {
 
-             ssh = new ScpClient(ip, Int32.Parse(port), user, pwd);
 
-         }
 
-         public DeviceSFTP(SFTPDeviceBody body)
 
-         {
 
-             ssh = new ScpClient(body.Ip, Int32.Parse(body.Port), body.UserName, body.Password);
 
-         }
 
-         #endregion
 
-         #region
 
-         /// <summary>
 
-         /// 连接SCP
 
-         /// </summary>
 
-         /// <returns>true成功</returns>
 
-         public bool Connect()
 
-         {
 
-             try
 
-             {
 
-                 if (!Connected)
 
-                 {
 
-                     ssh.Connect();
 
-                 }
 
-                 return true;
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message));
 
-             }
 
-         }
 
-         #endregion
 
-         #region 断开SCP
 
-         /// <summary>
 
-         /// 断开SCP
 
-         /// </summary> 
 
-         public bool Disconnect()
 
-         {
 
-             try
 
-             {
 
-                 if (ssh != null && Connected)
 
-                 {
 
-                     ssh.Disconnect();
 
-                 }
 
-                 return true;
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message));
 
-             }
 
-         }
 
-         #endregion
 
-         #region SFTP上传文件
 
-         /// <summary>
 
-         /// SCP上传文件
 
-         /// </summary>
 
-         /// <param name="localPath">本地路径</param>
 
-         /// <param name="remotePath">远程路径</param>
 
-         public int Put(string localPath, string remotePath)
 
-         {
 
-             try
 
-             {
 
-                 FileInfo file = new FileInfo(localPath);
 
-                 Connect();
 
-                 ssh.Upload(file, remotePath);
 
-                 Disconnect();
 
-                 return 1;
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 throw new Exception(string.Format("SFTP文件上传失败,原因:{0}", ex.Message));
 
-                 return 0;
 
-             }
 
-         }
 
-         #endregion
 
-         #region SFTP获取文件
 
-         /// <summary>
 
-         /// SCP获取文件
 
-         /// </summary>
 
-         /// <param name="remotePath">远程路径</param>
 
-         /// <param name="localPath">本地路径</param>
 
-         public void Get(string remotePath, DirectoryInfo localPath)
 
-         {
 
-             try
 
-             {
 
-                 Connect();
 
-                 ssh.Download(remotePath, localPath);
 
-                 Disconnect();
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 throw new Exception(string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
 
-             }
 
-         }
 
-         #endregion
 
-         public void DeleteFile(SFTPDeviceBody body, string filename)
 
-         {
 
-             using (SshClient sshClient = new SshClient(body.Ip, Int32.Parse(body.Port), body.UserName, body.Password))
 
-             {
 
-                 //连接
 
-                 sshClient.Connect();
 
-                 //执行命令
 
-                 //rm -rf /var/log/httpd/access
 
-                 var cmd = sshClient.RunCommand($"rm -f /card/user/sinumerik/data/prog/YGMESFILE/{filename}");
 
-                 //ExitStatus == 0 执行成功
 
-                 //
 
-                 if (cmd.ExitStatus == 0)
 
-                 {
 
-                     Console.WriteLine(cmd.Result);//执行结果
 
-                 }
 
-                 else
 
-                 {
 
-                     Console.WriteLine(cmd.Error);//错误信息
 
-                 }
 
-                 //断开连接
 
-                 sshClient.Disconnect();
 
-             }
 
-         }
 
-         #region SFTP删除文件
 
-         /// <summary>
 
-         /// SCP获取文件
 
-         /// </summary>
 
-         /// <param name="remotePath">远程路径</param>
 
-         /// <param name="localPath">本地路径</param>
 
-         public void test()
 
-         {
 
-             using (SshClient sshClient = new SshClient("192.168.170.205", Int32.Parse("22"), "manufact", "SUNRISE"))
 
-             {
 
-                 //连接
 
-                 sshClient.Connect();
 
-                 //执行命令
 
-                 var cmd = sshClient.RunCommand("cd /card/user/sinumerik/data/prog/YGMESFILE && ls");
 
-                 //ExitStatus == 0 执行成功
 
-                 //
 
-                 if (cmd.ExitStatus == 0)
 
-                 {
 
-                     Console.WriteLine(cmd.Result);//执行结果
 
-                 }
 
-                 else
 
-                 {
 
-                     Console.WriteLine(cmd.Error);//错误信息
 
-                 }
 
-                 //断开连接
 
-                 sshClient.Disconnect();
 
-             }
 
-         }
 
-         #endregion
 
-     }
 
-      
 
-     
 
- }
 
 
  |