Exemplo n.º 1
0
        private async System.Threading.Tasks.Task downloadAsync(SftpFile file, string destFile)
        {
            var connectionInfo = new ConnectionInfo(txtIp.Text, int.Parse(txtPort.Text), txtUser.Text, new PasswordAuthenticationMethod(txtUser.Text, txtPassword.Text));

            using (var client = new SftpClient(connectionInfo))
            {
                try
                {
                    client.Connect();

                    if (file.IsRegularFile)
                    {
                        using (var saveFile = File.OpenWrite(destFile))
                        {
                            double max = (double)file.Length;
                            this.progressBar1.Maximum = 100;
                            this.progressBar1.Show();
                            await client.DownloadAsync(file.FullName, saveFile, new Action <ulong>((o) =>
                            {
                                this.Invoke(new Action(() =>
                                {
                                    this.progressBar1.Value = (int)(((double)o / max) * 100);
                                    if ((double)o == max)
                                    {
                                        MessageBox.Show("下载完成!");
                                        this.progressBar1.Hide();
                                    }
                                }));
                            }));
                        }
                    }
                    else
                    {
                        MessageBox.Show("只允许下载单个文件!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("下载失败" + ex.Message);
                }
            }
        }