コード例 #1
0
        public void UploadViaSftp(string zipFileName, string sftpUrl, string userName, string password)
        {
            SFtp sftp = new SFtp();

            //  Any string automatically begins a fully-functional 30-day trial.
            bool success;

            success = sftp.UnlockComponent("HURRIYSSH_XAN0ZUeq9En4");
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  Set some timeouts, in milliseconds:
            sftp.ConnectTimeoutMs = 60000;
            sftp.IdleTimeoutMs    = 300000;

            //  Connect to the SSH server.
            //  The standard SSH port = 22
            //  The hostname may be a hostname or IP address.
            int    port;
            string hostname;

            hostname = sftpUrl;
            port     = 22;
            success  = sftp.Connect(hostname, port);
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  Authenticate with the SSH server.  Chilkat SFTP supports
            //  both password-based authenication as well as public-key
            //  authentication.  This example uses password authenication.
            success = sftp.AuthenticatePw(userName, password);
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  After authenticating, the SFTP subsystem must be initialized:
            success = sftp.InitializeSftp();
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  Open a file on the server for writing.
            //  "createTruncate" means that a new file is created; if the file already exists, it is opened and truncated.
            string handle;

            handle = sftp.OpenFile(Path.GetFileName(zipFileName), "writeOnly", "createTruncate");
            if (handle == null)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  Upload from the local file to the SSH server.
            success = sftp.UploadFile(handle, zipFileName);
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  Close the file.
            success = sftp.CloseHandle(handle);
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }
        }