C#利用SFTP实现上传下载
sftp是ftp协议的升级版本,是牺牲上传速度为代价,换取安全性能,本人开始尝试使用Tamir.SharpSSH.dll但它对新版本的openssh不支持,所有采用Ssh.Net方式需要依赖:Renci.SshNet.dll下载链接
//////SFTP操作类 /// publicclassSFTPHelper { #region字段或属性 privateSftpClientsftp; //////SFTP连接状态 /// publicboolConnected{get{returnsftp.IsConnected;}} #endregion #region构造 //////构造 /// ///IP /// 端口 /// 用户名 /// 密码 publicSFTPHelper(stringip,stringport,stringuser,stringpwd) { sftp=newSftpClient(ip,Int32.Parse(port),user,pwd); } #endregion #region连接SFTP /// ///连接SFTP /// ///true成功 publicboolConnect() { try { if(!Connected) { sftp.Connect(); } returntrue; } catch(Exceptionex) { //TxtLog.WriteTxt(CommonMethod.GetProgramName(),string.Format("连接SFTP失败,原因:{0}",ex.Message)); thrownewException(string.Format("连接SFTP失败,原因:{0}",ex.Message)); } } #endregion #region断开SFTP //////断开SFTP /// publicvoidDisconnect() { try { if(sftp!=null&&Connected) { sftp.Disconnect(); } } catch(Exceptionex) { //TxtLog.WriteTxt(CommonMethod.GetProgramName(),string.Format("断开SFTP失败,原因:{0}",ex.Message)); thrownewException(string.Format("断开SFTP失败,原因:{0}",ex.Message)); } } #endregion #regionSFTP上传文件 //////SFTP上传文件 /// ///本地路径 /// 远程路径 publicvoidPut(stringlocalPath,stringremotePath) { try { using(varfile=File.OpenRead(localPath)) { Connect(); sftp.UploadFile(file,remotePath); Disconnect(); } } catch(Exceptionex) { //TxtLog.WriteTxt(CommonMethod.GetProgramName(),string.Format("SFTP文件上传失败,原因:{0}",ex.Message)); thrownewException(string.Format("SFTP文件上传失败,原因:{0}",ex.Message)); } } #endregion #regionSFTP获取文件 /// ///SFTP获取文件 /// ///远程路径 /// 本地路径 publicvoidGet(stringremotePath,stringlocalPath) { try { Connect(); varbyt=sftp.ReadAllBytes(remotePath); Disconnect(); File.WriteAllBytes(localPath,byt); } catch(Exceptionex) { //TxtLog.WriteTxt(CommonMethod.GetProgramName(),string.Format("SFTP文件获取失败,原因:{0}",ex.Message)); thrownewException(string.Format("SFTP文件获取失败,原因:{0}",ex.Message)); } } #endregion #region删除SFTP文件 /// ///删除SFTP文件 /// ///远程路径 publicvoidDelete(stringremoteFile) { try { Connect(); sftp.Delete(remoteFile); Disconnect(); } catch(Exceptionex) { //TxtLog.WriteTxt(CommonMethod.GetProgramName(),string.Format("SFTP文件删除失败,原因:{0}",ex.Message)); thrownewException(string.Format("SFTP文件删除失败,原因:{0}",ex.Message)); } } #endregion #region获取SFTP文件列表 /// ///获取SFTP文件列表 /// ///远程目录 /// 文件后缀 /// publicArrayListGetFileList(stringremotePath,stringfileSuffix) { try { Connect(); varfiles=sftp.ListDirectory(remotePath); Disconnect(); varobjList=newArrayList(); foreach(varfileinfiles) { stringname=file.Name; if(name.Length>(fileSuffix.Length+1)&&fileSuffix==name.Substring(name.Length-fileSuffix.Length)) { objList.Add(name); } } returnobjList; } catch(Exceptionex) { //TxtLog.WriteTxt(CommonMethod.GetProgramName(),string.Format("SFTP文件列表获取失败,原因:{0}",ex.Message)); thrownewException(string.Format("SFTP文件列表获取失败,原因:{0}",ex.Message)); } } #endregion #region移动SFTP文件 /// ///移动SFTP文件 /// ///旧远程路径 /// 新远程路径 publicvoidMove(stringoldRemotePath,stringnewRemotePath) { try { Connect(); sftp.RenameFile(oldRemotePath,newRemotePath); Disconnect(); } catch(Exceptionex) { //TxtLog.WriteTxt(CommonMethod.GetProgramName(),string.Format("SFTP文件移动失败,原因:{0}",ex.Message)); thrownewException(string.Format("SFTP文件移动失败,原因:{0}",ex.Message)); } } #endregion }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。