SSH.NET上传整个文件夹我在C#2015中使用SSH.NET。通过这种方法,我可以将文件上传到我的SFTP服务器。publicvoidupload(){constintport=22;const字符串主机="*****";conststring用户名="*****";conststringpassword="*****";conststringworkingdirectory="*****";stringuploadfolder=@"C:testfile.txt";Console.WriteLine("创建客户端并连接");使用(varclient=newSftpClient(host,port,username,password)){client.Connect();Console.WriteLine("连接到{0}",主机);client.ChangeDirectory(工作目录);Console.WriteLine("更改目录到{0}",workingdirectory);使用(varfileStream=newFileStream(uploadfolder,FileMode.Open)){Console.WriteLine("上传{0}({1:N0}字节)",uploadfolder,fileStream.Length);client.BufferSize=4*1024;//绕过Payload错误大文件client.UploadFile(fileStream,Path.GetFileName(uploadfolder));这适用于单个文件。现在我想上传整个文件夹/目录。现在有人如何实现这一目标?没有神奇的方法。一定要枚举文件,一一上传:以上是C#学习教程:SSH.NET上传文件夹全部内容分享。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——voidUploadDirectory(SftpClientclient,stringlocalPath,stringremotePath){Console.WriteLine("上传目录{0}到{1}",本地路径,远程路径);IEnumerableinfos=newDirectoryInfo(localPath).EnumerateFileSystemInfos();foreach(FileSystemInfoinfoininfos){if(info.Attributes.HasFlag(FileAttributes.Directory)){stringsubPath=remotePath+"/"+info.Name;如果(!client.Exists(subPath)){client.CreateDirectory(subPath);}UploadDirectory(client,info.FullName,remotePath+"/"+info.Name);}else{using(StreamfileStream=newFileStream(info.FullName,FileMode.Open)){Console.WriteLine("上传{0}({1:N0}字节)",info.FullName,((FileInfo)info)。长度);client.UploadFile(fileStream,remotePath+"/"+info.Name);}}}}本文收集自网络,不代表任何内容,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:
