WinSCP.NET Collection: How to Download Directories

I wrote a C # application that uses System.IO.GetDirectoires() and System.IO.GetFiles()

Now I need to convert this to use SFTP. I have experience with PutFiles and GetFiles of the WinSCP.NET assembly, but I cannot figure out how to get the list of directories. There is GetFiles winscp.exe file, which I can use for files, but there is no way to get directories, as far as I can tell. Does anyone have a way to do this or have a library that is easier to work with.

 // Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = "example.com", UserName = "user", Password = "mypassword", SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" }; using (Session session = new Session()) { // Connect session.Open(sessionOptions); } 
+5
source share
2 answers

Session.GetFiles of the WinSCP.NET assembly loads both files and subfolders .


Actually, you should explicitly indicate when you do not want to download them.

See How to transfer (or synchronize) a directory not recursively?


If you want to list subfolders in a remote directory, use Session.EnumerateRemoteFiles with EnumerationOptions.MatchDirectories and filter the result set for entries with RemoteFileInfo.IsDirectory :

 IEnumerable<RemoteFileInfo> directories = session.EnumerateRemoteFiles(path, null, EnumerationOptions.MatchDirectories). Where(file => file.IsDirectory); 

But then again, you don’t have to do this to load directories, Session.GetFiles does it for you.

+3
source

Try something like this

  // Connect session.Open(sessionOptions); RemoteDirectoryInfo directory = session.ListDirectory("/"); foreach (RemoteFileInfo fileInfo in directory.Files) { Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}", fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime); } 

Also try

 string dumpCommand = "ls"; session.ExecuteCommand(dumpCommand) 
+2
source

Source: https://habr.com/ru/post/1237415/


All Articles