Search code examples
c#.netsftpwinscpwinscp-net

WinSCP PutFileToDirectory says the remote file does not exist but it Does


I am getting an error from WinSCP that the file does not exist in the method Session.PutFileToDirectory. But the file does exist in fact on the line above this call I did GetFileInfo for the same file and it worked just fine. So the error is isolated to PutFileToDirectory. But I have no ideas how to even debug this or what could be wrong.

WinSCP.Session session = new WinSCP.Session();

string LocalDirectory = @"O:\FISCAL SVS\FILES\WStorey\automation\sftp\mcpay\";
string RemoteDirectory = @"";
string RemoteFileName = @"";
string RemoteFilePath = string.Format(@"{0}/{1}", RemoteDirectory, RemoteFileName);

int Protocol = 1;
int PortNumber = 22;
string HostName = "";
string UserName = "";
string Password = "";
string Fingerprint = "";

WinSCP.SessionOptions options = new WinSCP.SessionOptions()
{
    Protocol = (WinSCP.Protocol)Protocol,
    PortNumber = PortNumber,
    HostName = HostName,
    UserName = UserName,
    Password = Password,
    SshHostKeyFingerprint = Fingerprint
};

session.Open(options);

WinSCP.RemoteFileInfo fileInfo = session.GetFileInfo(RemoteFilePath);
session.PutFileToDirectory(RemoteFilePath, LocalDirectory);

session.Close();

Solution

  • Your code is wrong. How it is wrong is hard to say as you didn't tell us what you want the code to do. From the fact that you check the remote file presence before the operation, I guess that you want to download the remote file. But Session.PutFileToDirectory uploads a file. If you want to download the file, use Session.GetFileToDirectory:

    session.GetFileToDirectory(RemoteFilePath, LocalDirectory);