0

I'm implementing the WinSCP .NET Assembly for SFTP functionality. I am able to establish a session, list directories and download files to \bin\x86\Debug, but for some reason I am unable to download files into directory bin\x86\Debug\edi on my local drive.

Download code:

public static List<string> GetSFTP(string host, string user, string password, int port, string source, string dest, string remoteDest)
{
    List<string> filenames = new List<string>();
    System.IO.Directory.CreateDirectory(dest);

    SessionOptions winSCPSessionOptions = new SessionOptions();
    winSCPSessionOptions.HostName = host;
    winSCPSessionOptions.Password = password;
    winSCPSessionOptions.PortNumber = port;
    winSCPSessionOptions.UserName = user;
    winSCPSessionOptions.Protocol = WinSCP.Protocol.Sftp;

    // This is a kind of dangerous setting, but as we do not have the host
    // key fingerprints this allows us to connect without complaint.
    winSCPSessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = true;

    Session session = new Session();
    session.Open(winSCPSessionOptions);
    RemoteDirectoryInfo remoteDirInfo = session.ListDirectory(remoteDest);
    foreach (RemoteFileInfo fileInfo in remoteDirInfo.Files)
    {
        if (fileInfo.Name.Equals(".") || fileInfo.Name.Equals("..")) { continue; }
        Console.WriteLine("{0}", remoteDest + fileInfo.Name);
        try
        {
            TransferOperationResult result = session.GetFiles(remoteDest + fileInfo.Name, dest+fileInfo.Name);
            if (!result.IsSuccess)
            {
                Console.WriteLine("Failed to download!");
                foreach (SessionException ex in result.Failures)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            else
            {
                Console.WriteLine("Successfully downloaded file!");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        filenames.Add(fileInfo.Name);
    }

    return filenames;
}

Called via:

GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "edi/", "/ProcessedFiles/Processed/");

I've tried:

GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "/edi/", "/ProcessedFiles/Processed/");
GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "\\edi\\", "/ProcessedFiles/Processed/");
GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "edi\\", "/ProcessedFiles/Processed/");

In all cases I get a varient of: %2Fedi%2Ffilename.txt downloaded to my working directory, not the specified path.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robert H
  • 11,520
  • 18
  • 68
  • 110
  • What the "dest+fileInfo.Name" statement looks like in debug ? – cubitouch Jan 22 '14 at 20:11
  • @cubitouch Dest is `edi/` and filename is `xxxx.ffv` - output into a string object it looks like : `"edi/XXXX_56981769220131028142331306-0001-0000021-0202.ffv"` – Robert H Jan 22 '14 at 20:23
  • And what exactly download this statement "GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "\\edi\\", "/ProcessedFiles/Processed/");" ? What are exactly the variants you are talking about ? – cubitouch Jan 22 '14 at 20:25
  • @cubitouch Not sure what you mean by your last comment - I will not provide credentials into my SFTP server for ppl to hack away on. The varient's I meantioned above: for each slash in my dest directory (/edi/, \\edi\\) it is replaced by a %2F or a similar representation of the slash. – Robert H Jan 22 '14 at 20:40
  • I'm asking you to provide a complete question background more precise that "I get a varient of '...'" providing 4 use cases before that... I used this wrapper few weeks back and I didn't had this kind of issues. What I was asking is the exact file path generated for each downloads you performed. – cubitouch Jan 22 '14 at 20:44
  • @cubitouch The above file in my first comment translates on disk to: `C:\Users\roberth\Programming_Projects\VisualStudio\vsOnline\MarketplaceIntegrator\bin\x86\Debug\edi%2Fxxxx_56981769220131028142331306-0001-0000021-0202.ffv` I'm expecting it to goto `C:\Users\roberth\Programming_Projects\VisualStudio\vsOnline\MarketplaceIntegrator\bin\x86\Debug\edi\xxxx_56981769220131028142331306-0001-0000021-0202.ffv` – Robert H Jan 22 '14 at 20:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45838/discussion-between-cubitouch-and-robert-h) – cubitouch Jan 22 '14 at 20:58

1 Answers1

1

This code should work:

GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "edi\\", "/ProcessedFiles/Processed/");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cubitouch
  • 1,929
  • 15
  • 28