Reputation: 127
I found WinSCP as a great solution for integrating SFTP download with an existing process. However, I have not been able to locate the types of errors that can be thrown by the .NET wrapper.
I like to have granular error processing where I can but without knowing what kinds of errors the DLL can throw I need to use more broad error trapping. I am running the transfer in a background worker thread and the UI allows users to add files to the processing queue while the app is running. The code below simplifies the process of getting the source and destination paths.
lstFTM
is the listbox holding files to move.
lstRes
is for the logging of each attempt to get
private void DoTransfer()
{
// Set up session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "ftp.somesite.com",
UserName = "user",
Password = "pass",
SshHostKeyFingerprint = "ssh-rsa 2048 ABC123xxx and so on",
};
using (Session session = new Session())
{
// Connect
try
{
session.Open(sessionOptions);
// Transfer files
const string remotePath = @"\\NAS\path\*";
foreach (string _s in lstFTM.Items)
{
try
{
session.GetFiles(_s, remotePath).Check();
lstRes.Items.Add(string.Format("{0} downloaded on {1}", _s, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")));
}
catch (Exception iex)
{
lstRes.Items.Add(string.Format("file transfer failed for {0} - reported error {1}", _s, iex.Message));
}
}
}
catch (Exception ex)
{
lstRes.Items.Add("session did not open (" + ex.Message + ")");
}
}
}
This works well but obviously I would prefer to handle different types of exceptions more discreetly (File IO vs Source not Found vs Permissions and so on)
Upvotes: 3
Views: 1019
Reputation: 202242
Each method of WinSCP .NET assembly documents what exception it can throw.
For example for Session.GetFiles
see:
https://winscp.net/eng/docs/library_session_getfiles#exceptions
Though unfortunately, it will not really help you with distinguishing "File IO vs Source not Found vs Permissions and so on".
That is due to the internal implementation of the assembly.
Upvotes: 1