Sharing Download/Upload methods in BaseClient

May 7, 2013 at 12:50 PM
actually i'm writing a clr for download and upload file through ms sql.
Both scp and sftp support upload and download. It could be a good approach to write a virtual method for upload and download in BaseClient. So if i like to switch the protocol, i just need to get the base class and execute download/upload method.

right now, i need something like this.
Boolean isSftp = (transferProtocol.Value.ToLower() == "sftp");
            using (Renci.SshNet.BaseClient ssh = isSftp == true ? 
                (new Renci.SshNet.SftpClient(new Renci.SshNet.PasswordConnectionInfo(server.Value, 22, user.Value, password.Value)) as Renci.SshNet.BaseClient) :
                new Renci.SshNet.ScpClient(new Renci.SshNet.PasswordConnectionInfo(server.Value, 22, user.Value, password.Value))
            )
            {
                ssh.Connect();
                //sftp.ChangeDirectory(System.IO.Path.GetDirectoryName(source.Value));
                //if (sftp.Exists(source.Value) == false)
                //    throw new Exception(String.Concat("File: ", source, " do not exists on the remote server"));
                System.IO.FileStream file = null;
                using (file = System.IO.File.Exists(destination.Value) == false ? System.IO.File.OpenWrite(destination.Value) : System.IO.File.Create(destination.Value))
                {
                    if (isSftp == true)
                        (ssh as Renci.SshNet.SftpClient).DownloadFile(source.Value, file);
                    else
                        (ssh as Renci.SshNet.ScpClient).Download(source.Value, file);
                }
                //delete only if sftp
                if (delete.IsTrue == true && isSftp == true)
                    (ssh as Renci.SshNet.SftpClient).Delete(source.Value);
            }