引用第三方库:Ssh .Net
try { using (var client = new SftpClient(ftpHost, ftpPort, ftpUsername, ftpPassword)) { client.Connect(); IEnumerable<ISftpFile> fileEnumerable = client.ListDirectory(remoteDirectory, null).Where(g => !g.IsDirectory & g.Name.Contains(dateStr));//获取到文件列表 foreach (var file in fileEnumerable) { var filename = $"{localDirectory}/{file.Name}";//本地需要保存文件的绝对路径 if (!File.Exists(filename)) { using (var localFile = File.OpenWrite(filename)) client.DownloadFile(file.FullName, localFile); downloadFiles.Add(file.Name); } } client.Disconnect(); } } catch (SocketException socketExc) { return new DownloadFileMessage { Code = DownloadFileCode.Faile, Message = "sftp地址连接超时(" + ftpHost + ")" + socketExc.Message }; } catch (SshAuthenticationException authenExc) { return new DownloadFileMessage { Code = DownloadFileCode.Faile, Message = "sftp账号或密码错误(" + ftpUsername + "/" + ftpPassword + ")" + authenExc.Message }; }
try { using (var client = new SftpClient(ftpHost, ftpPort, ftpUsername, ftpPassword)) { client.Connect(); var byt = client.ReadAllBytes(remoteZipPath); //remoteZipPath是ftp上的路径 File.WriteAllBytes(localZipPath, byt); //localZipPath是本地的绝对路径 client.Disconnect(); } } catch (SocketException socketExc) { return "\"sftp地址连接超时(\" + ftpHost + \")\" + socketExc.Message"; } catch (SshAuthenticationException authenExc) { return "sftp账号或密码错误(" + ftpUsername + "/" + ftpPassword + ")" + authenExc.Message; } catch (SftpPathNotFoundException pathNotFoundExc) { return "sftp没有找到对应的文件(" + remoteZipPath + ")" + pathNotFoundExc.Message; }
标签:core,asp,return,SFTP,catch,client,var,Message,sftp From: https://www.cnblogs.com/fireicesion/p/18283198