Renci.SshNet.ScpClient.Download C# (CSharp) Method

Download() public method

Downloads the specified file from the remote host to the stream.
Method calls made by this method to destination, may under certain conditions result in exceptions thrown by the stream.
is null or contains only whitespace characters. is null.
public Download ( string filename, Stream destination ) : void
filename string Remote host file name.
destination Stream The stream where to download remote file.
return void
        public void Download(string filename, Stream destination)
        {
            if (filename.IsNullOrWhiteSpace())
                throw new ArgumentException("filename");

            if (destination == null)
                throw new ArgumentNullException("destination");

            using (var input = ServiceFactory.CreatePipeStream())
            using (var channel = Session.CreateChannelSession())
            {
                channel.DataReceived += (sender, e) => input.Write(e.Data, 0, e.Data.Length);
                channel.Open();

                //  Send channel command request
                channel.SendExecRequest(string.Format("scp -f \"{0}\"", filename));
                SendConfirmation(channel); //  Send reply

                var message = ReadString(input);
                var match = FileInfoRe.Match(message);

                if (match.Success)
                {
                    //  Read file
                    SendConfirmation(channel); //  Send reply

                    var mode = match.Result("${mode}");
                    var length = long.Parse(match.Result("${length}"));
                    var fileName = match.Result("${filename}");

                    InternalDownload(channel, input, destination, fileName, length);
                }
                else
                {
                    SendConfirmation(channel, 1, string.Format("\"{0}\" is not valid protocol message.", message));
                }
            }
        }

Same methods

ScpClient::Download ( string directoryName, DirectoryInfo directoryInfo ) : void
ScpClient::Download ( string filename, FileInfo fileInfo ) : void

Usage Example

Beispiel #1
0
        private bool download()
        {
            folderBrowserDialog.Description = "Choose Where To Download";
              if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
            if (listView1.SelectedItems.Count > 0) {
              try {
            listView1.Enabled = false;
            ScpClient scpClient =
                new ScpClient(this.sftpClient.ConnectionInfo);
            scpClient.Connect();
            foreach (ListViewItem lvi in listView1.SelectedItems) {
              SftpFile sf = (SftpFile)(lvi.Tag);

              if (sf.IsDirectory) {
                scpClient.Download(sf.FullName,
                    new System.IO.DirectoryInfo(folderBrowserDialog.SelectedPath).CreateSubdirectory(sf.Name));
              } else {
                scpClient.Download(sf.FullName,
                    new System.IO.FileInfo(folderBrowserDialog.SelectedPath + "\\" + sf.Name));
              }
            }
            scpClient.Disconnect();
              } catch (Exception ex) {
              } finally {
            listView1.Enabled = true;
              }
            }
              }
              return true;
        }
All Usage Examples Of Renci.SshNet.ScpClient::Download