Search code examples
c#ssisftpssis-2012

Get File Size from FTP (in SSIS, using Dts.Connections)


I have an issue sending big file to an FTP site and I would like to check the file size after the transfer (sometimes it works sometimes it fails). The transfer is within an SSIS and I'm using Dts.Connections in C#.

My code:

public long TransferFile(string file)
{
    long filesize = 0L;

    try
    {
        string[] newfile = new[] { file };

        ConnectionManager ftpCM = Dts.Connections["ftp_server"];
        string remoteDir = Dts.Variables["FtpWorkingDirectory"].Value.ToString();

        FtpClientConnection ftpClient = new FtpClientConnection(ftpCM.AcquireConnection(null));
        ftpClient.UsePassiveMode = true;
        ftpClient.Connect();
        ftpClient.Retries = 10;
        ftpClient.SetWorkingDirectory(remoteDir);

        ftpClient.SendFiles(newfile, remoteDir, true, false);

        ftpClient.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return filesize;
}

I found examples using FtpWebRequest but I don't have the ftp uri available so I don't see how to use this method. How can I get this file size?

UPDATE: Adding this:

        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpCM.ConnectionString + remoteDir + "/" + file));
        request.Proxy = null;

        DtsProperty ServerUsername = ftpCM.Properties["ServerUserName"];
        DtsProperty ServerPassword = ftpCM.Properties["ServerPassword"];
        request.Credentials = new NetworkCredential(ServerUsername.GetValue(ftpCM).ToString(), ServerPassword.GetValue(ftpCM).ToString());
        request.Method = WebRequestMethods.Ftp.GetFileSize;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        filesize = response.ContentLength;
        response.Close();

I get error: The property ServerPassword is write-only.


Solution

  • The final solution was to store the username and password in Variables instead of connection parameters.

    And with the following code:

    public long TransferFile(string file)
    {
        long filesize = 0L;
    
        try
        {
            string[] newfile = new[] { file };
    
            ConnectionManager ftpCM = Dts.Connections["ftp_server"];
            string remoteDir = Dts.Variables["FtpWorkingDirectory"].Value.ToString();
            string ServerUsername = Dts.Variables["ServerUsername"].Value.ToString();
            string ServerPassword = Dts.Variables["ServerPassword"].Value.ToString();
    
            FtpClientConnection ftpClient = new FtpClientConnection(ftpCM.AcquireConnection(null));
            ftpClient.UsePassiveMode = true;
            ftpClient.ServerUserName = ServerUsername;
            ftpClient.ServerPassword = ServerPassword;
    
            ftpClient.Connect();
            ftpClient.Retries = 10;
            ftpClient.SetWorkingDirectory(remoteDir);
    
            ftpClient.SendFiles(newfile, remoteDir, true, false);
    
            ftpClient.Close();
    
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpCM.ConnectionString + remoteDir + "/" + Path.GetFileName(file)));
            request.Proxy = null;
    
            request.Credentials = new NetworkCredential(ServerUsername, ServerPassword);
            request.Method = WebRequestMethods.Ftp.GetFileSize;
    
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            filesize = response.ContentLength;
            response.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
        return filesize;
    }