This is implemented using FtpWebRequest which can only handle FTP/FTPS. This will NOT WORK for SFTP, which is a completely different protocol.

        private const int MILLISECONDS_IN_SECOND = 1000;

        private FtpState ftpState = null;
        private String ftpLocation = String.Empty;       
        private String fileName = String.Empty;
        private String renameFileName = String.Empty;
        private String localFileLocation = String.Empty;
        private String userName = "anonymous";
        private String password = "anonymous";
        private bool enableSSL = false;
        private int ftpTimeoutSeconds = System.Threading.Timeout.Infinite;

        public virtual void TransferFileToFtp()
        {
            ftpState = null;
            ftpState = new FtpState();

            ManualResetEvent waitObject;
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpLocation + @"/" + fileName);
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // The request is anonymous by default; the credential does not have to be specified. 
            // The example specifies the credential only to control how actions are logged on the server.

            request.Credentials = new NetworkCredential(userName, password);
            request.EnableSsl = enableSSL;
            request.UsePassive = false;

            // Store the request in the object that we pass into the asynchronous operations.
            ftpState.Request = request;
            ftpState.FileName = String.Format("{0}\\{1}", localFileLocation, fileName);

            // Get the event to wait on.
            waitObject = ftpState.OperationComplete;

            // Asynchronously get the stream for the file contents.
            request.BeginGetRequestStream(new AsyncCallback(EndTransferFileCallback), ftpState);

            // Block the current thread until all operations are complete (up to _ftpTimeoutSeconds seconds)
            if(!waitObject.WaitOne(ftpTimeoutSeconds, false))
            {
                throw new Exception("Couldn't complete transfer. Operation timeout.");
            }

            // The operations either completed or threw an exception.
            if(ftpState.OperationException != null)
            {
                throw ftpState.OperationException;
            }
        }
    public class FtpState
    {
        private ManualResetEvent wait;
        private FtpWebRequest request;
        private string fileName;
        private string localFileLocation;
        private Exception operationException = null;
        private List<String> directiryList = new List<String>();
        string status;

        public FtpState()
        {
            wait = new ManualResetEvent(false);
        }

        public ManualResetEvent OperationComplete
        {
            get { return wait; }
        }

        public FtpWebRequest Request
        {
            get { return request; }
            set { request = value; }
        }

        public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        public string LocalFileLocation
        {
            get { return localFileLocation; }
            set { localFileLocation = value; }
        }        

        public List<String> DirectiryList
        {
            get { return directiryList; }
            set { directiryList = value; }
        }        

        public Exception OperationException
        {
            get { return operationException; }
            set { operationException = value; }
        }

        public string StatusDescription
        {
            get { return status; }
            set { status = value; }
        }
    }
Last modified: July 2, 2019

Author

Comments

Write a Reply or Comment