SFTP上传下载(C#)
sftp是ftp协议的升级版本,是牺牲上传速度为代价,换取安全性能,本人开始尝试使用Tamir.SharpSSH.dll但它对新版本的openssh 不支持,所有采用Ssh.Net方式 需要依赖:Renci.SshNet.dll 链接: http://pan.baidu.com/s/1nvuHFVN 密码: hh49
1 /// <summary> 2 /// SFTP操作类 3 /// </summary> 4 public class SFTPHelper 5 { 6 #region 字段或属性 7 private SftpClient sftp; 8 /// <summary> 9 /// SFTP连接状态 10 /// </summary> 11 public bool Connected { get { return sftp.IsConnected; } } 12 #endregion 13 14 #region 构造 15 /// <summary> 16 /// 构造 17 /// </summary> 18 /// <param name="ip">IP</param> 19 /// <param name="port">端口</param> 20 /// <param name="user">用户名</param> 21 /// <param name="pwd">密码</param> 22 public SFTPHelper(string ip, string port, string user, string pwd) 23 { 24 sftp = new SftpClient(ip, Int32.Parse(port), user, pwd); 25 } 26 #endregion 27 28 #region 连接SFTP 29 /// <summary> 30 /// 连接SFTP 31 /// </summary> 32 /// <returns>true成功</returns> 33 public bool Connect() 34 { 35 try 36 { 37 if (!Connected) 38 { 39 sftp.Connect(); 40 } 41 return true; 42 } 43 catch (Exception ex) 44 { 45 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("连接SFTP失败,原因:{0}", ex.Message)); 46 throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message)); 47 } 48 } 49 #endregion 50 51 #region 断开SFTP 52 /// <summary> 53 /// 断开SFTP 54 /// </summary> 55 public void Disconnect() 56 { 57 try 58 { 59 if (sftp != null && Connected) 60 { 61 sftp.Disconnect(); 62 } 63 } 64 catch (Exception ex) 65 { 66 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("断开SFTP失败,原因:{0}", ex.Message)); 67 throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message)); 68 } 69 } 70 #endregion 71 72 #region SFTP上传文件 73 /// <summary> 74 /// SFTP上传文件 75 /// </summary> 76 /// <param name="localPath">本地路径</param> 77 /// <param name="remotePath">远程路径</param> 78 public void Put(string localPath, string remotePath) 79 { 80 try 81 { 82 using (var file = File.OpenRead(localPath)) 83 { 84 Connect(); 85 sftp.UploadFile(file, remotePath); 86 Disconnect(); 87 } 88 } 89 catch (Exception ex) 90 { 91 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件上传失败,原因:{0}", ex.Message)); 92 throw new Exception(string.Format("SFTP文件上传失败,原因:{0}", ex.Message)); 93 } 94 } 95 #endregion 96 97 #region SFTP获取文件 98 /// <summary> 99 /// SFTP获取文件 100 /// </summary> 101 /// <param name="remotePath">远程路径</param> 102 /// <param name="localPath">本地路径</param> 103 public void Get(string remotePath, string localPath) 104 { 105 try 106 { 107 Connect(); 108 var byt = sftp.ReadAllBytes(remotePath); 109 Disconnect(); 110 File.WriteAllBytes(localPath, byt); 111 } 112 catch (Exception ex) 113 { 114 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件获取失败,原因:{0}", ex.Message)); 115 throw new Exception(string.Format("SFTP文件获取失败,原因:{0}", ex.Message)); 116 } 117 118 } 119 #endregion 120 121 #region 删除SFTP文件 122 /// <summary> 123 /// 删除SFTP文件 124 /// </summary> 125 /// <param name="remoteFile">远程路径</param> 126 public void Delete(string remoteFile) 127 { 128 try 129 { 130 Connect(); 131 sftp.Delete(remoteFile); 132 Disconnect(); 133 } 134 catch (Exception ex) 135 { 136 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件删除失败,原因:{0}", ex.Message)); 137 throw new Exception(string.Format("SFTP文件删除失败,原因:{0}", ex.Message)); 138 } 139 } 140 #endregion 141 142 #region 获取SFTP文件列表 143 /// <summary> 144 /// 获取SFTP文件列表 145 /// </summary> 146 /// <param name="remotePath">远程目录</param> 147 /// <param name="fileSuffix">文件后缀</param> 148 /// <returns></returns> 149 public ArrayList GetFileList(string remotePath, string fileSuffix) 150 { 151 try 152 { 153 Connect(); 154 var files = sftp.ListDirectory(remotePath); 155 Disconnect(); 156 var objList = new ArrayList(); 157 foreach (var file in files) 158 { 159 string name = file.Name; 160 if (name.Length > (fileSuffix.Length + 1) && fileSuffix == name.Substring(name.Length - fileSuffix.Length)) 161 { 162 objList.Add(name); 163 } 164 } 165 return objList; 166 } 167 catch (Exception ex) 168 { 169 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message)); 170 throw new Exception(string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message)); 171 } 172 } 173 #endregion 174 175 #region 移动SFTP文件 176 /// <summary> 177 /// 移动SFTP文件 178 /// </summary> 179 /// <param name="oldRemotePath">旧远程路径</param> 180 /// <param name="newRemotePath">新远程路径</param> 181 public void Move(string oldRemotePath, string newRemotePath) 182 { 183 try 184 { 185 Connect(); 186 sftp.RenameFile(oldRemotePath, newRemotePath); 187 Disconnect(); 188 } 189 catch (Exception ex) 190 { 191 // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件移动失败,原因:{0}", ex.Message)); 192 throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", ex.Message)); 193 } 194 } 195 #endregion 196 197 }