Granados.Poderosa.SCP.SCPClient.Upload C# (CSharp) Method

Upload() public method

Upload files or directories.

Unfortunately, Granados sends a command line in the ASCII encoding. So the "remotePath" must be an ASCII text.

public Upload ( string localPath, string remotePath, bool recursive, bool preserveTime, Cancellation cancellation, SCPFileTransferProgressDelegate progressDelegate ) : void
localPath string Local path (Windows' path)
remotePath string Remote path (Unix path)
recursive bool Specifies recursive mode
preserveTime bool Specifies to preserve time of the directory or file.
cancellation Cancellation An object to request the cancellation. Set null if the cancellation is not needed.
progressDelegate SCPFileTransferProgressDelegate Delegate to notify progress. Set null if notification is not needed.
return void
        public void Upload(string localPath, string remotePath, bool recursive, bool preserveTime,
                            Cancellation cancellation,
                            SCPFileTransferProgressDelegate progressDelegate)
        {
            if (!IsAscii(remotePath))
                throw new SCPClientException("Remote path must consist of ASCII characters.");

            bool isDirectory = Directory.Exists(localPath);
            if (!File.Exists(localPath) && !isDirectory)
                throw new SCPClientException("File or directory not found: " + localPath);

            if (isDirectory && !recursive)
                throw new SCPClientException("Cannot copy directory in non-recursive mode");

            string absLocalPath = Path.GetFullPath(localPath);

            string command = "scp -t ";
            if (recursive)
                command += "-r ";
            if (preserveTime)
                command += "-p ";
            command += EscapeUnixPath(remotePath);

            using (SCPChannelStream stream = new SCPChannelStream()) {
                stream.Open(_connection, command, _protocolTimeout);
                CheckResponse(stream);

                if (isDirectory) {  // implies recursive mode
                    UploadDirectory(absLocalPath, stream, preserveTime, cancellation, progressDelegate);
                }
                else {
                    UploadFile(absLocalPath, stream, preserveTime, cancellation, progressDelegate);
                }
            }
        }