Search code examples
c#winscpwinscp-net

Why is file's local directory being created to remote on upload


I am creating a .NET service application that will provide FTPS functionality.

When uploading a local file to the remote location, the local file's immediate directory is recreated in the remote location.

I suspect the mask I am using is part of my problem, but I'm not sure. What is causing the directory to be recreated in the remote location?

I have tried altering the mask based on the documentation.

public static void Upload()
{
    using (Session session = new Session())
    {
        /* Connect
         * */
        session.Open(GetSessionOptions());

        /* Upload files
         * */
        string localFilePath = "C:\\WinScpTest\\test\\";
        string remoteFilePath = $"/Remote/{DateTime.Now.Year.ToString()}/";
        bool removeOrig = false;
        TransferOptions transferOpts = new TransferOptions
        {
            TransferMode = TransferMode.Binary,
            FileMask = "*.txt"
        };
        TransferOperationResult result = session.PutFiles(localFilePath, remoteFilePath, removeOrig, transferOpts);

    }
}

I would expect a local file C:\WinScpTest\test\file.txt to upload to /Remote/2019/file.txt. Instead, I see this /Remote/2019/test/file.txt


Solution

  • As described in the comment, depending on the tool being used for copying or uploading files, removing or adding a trailing directory delimiter determines wether the parent directory is to be included or not.

    In most cases, such as this one, a trailing directory seperator on the target path indicates that the parent directory is to be created as well. Removing it indicates that the files shall be uploaded directly into the given folder.

    In your case that would mean changing $"/Remote/{DateTime.Now.Year.ToString()}/"; to $"/Remote/{DateTime.Now.Year.ToString()}";