gpt4 book ai didi

c# - 使用 SSH.NET 在进度栏中显示文件下载进度

转载 作者:行者123 更新时间:2023-12-02 12:16:22 27 4
gpt4 key购买 nike

我想在我的 ProgressBar 上显示下载过程的进度。我尝试做类似 this code for upload 的事情,但我失败了。这是我失败的尝试的示例

private void button5_Click(object sender, EventArgs e)
{
Task.Run(() => Download());
}

private void Download()
{
try
{
int Port = (int)numericUpDown1.Value;
string Host = comboBox1.Text;
string Username = textBox3.Text;
string Password = textBox4.Text;
string SourcePath = textBox5.Text;
string RemotePath = textBox6.Text;
string FileName = textBox7.Text;

using (var file = File.OpenWrite(SourcePath + FileName))
using (var Stream = new FileStream(SourcePath + FileName, FileMode.Open))
using (var Client = new SftpClient(Host, Port, Username, Password))
{
Client.Connect();
progressBar1.Invoke((MethodInvoker)
delegate
{
progressBar1.Maximum = (int)Stream.Length;
});
Client.DownloadFile(RemotePath + FileName, /*file*/ Stream, DownloadProgresBar);
Client.Disconnect();
}
}
catch (Exception Ex)
{
System.Windows.Forms.MessageBox.Show(Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DownloadProgresBar(ulong Downloaded)
{
progressBar1.Invoke((MethodInvoker)
delegate
{
progressBar1.Value = (int)Downloaded;
});
}

提前谢谢

最佳答案

正如您所做的那样,类似于 displaying progress of file upload 的代码,您必须为 SftpClient.DownloadFiledownloadCallback 参数提供回调。

public void DownloadFile(
string path, Stream output, Action<ulong> downloadCallback = null)

您还可以在后台线程上正确下载。或者,您可以使用异步上传 (SftpClient.BeginDownloadFile)。

哪里出了问题并且需要改变:

  • 您必须打开/创建本地文件以进行写入 (FileMode.Create)。
  • 您必须检索远程文件的大小,而不是本地文件的大小(尚不存在)。使用SftpClient.GetAttributes
<小时/>

使用后台线程(任务)的示例:

private void button1_Click(object sender, EventArgs e)
{
// Run Download on background thread
Task.Run(() => Download());
}

private void Download()
{
try
{
int Port = 22;
string Host = "example.com";
string Username = "username";
string Password = "password";
string RemotePath = "/remote/path/";
string SourcePath = @"C:\local\path\";
string FileName = "download.txt";

string SourceFilePath = SourcePath + FileName;
using (var stream = new FileStream(SourceFilePath, FileMode.Create))
using (var client = new SftpClient(Host, Port, Username, Password))
{
client.Connect();
string RemoteFilePath = RemotePath + FileName;
SftpFileAttributes attrs = client.GetAttributes(RemoteFilePath);
// Set progress bar maximum on foreground thread
int max = (int)attrs.Size;
progressBar1.Invoke(
(MethodInvoker)delegate { progressBar1.Maximum = max; });
// Download with progress callback
client.DownloadFile(RemoteFilePath, stream, DownloadProgresBar);
MessageBox.Show("Download complete");
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}

private void DownloadProgresBar(ulong uploaded)
{
// Update progress bar on foreground thread
progressBar1.Invoke(
(MethodInvoker)delegate { progressBar1.Value = (int)uploaded; });
}

enter image description here

<小时/>

对于上传,请参阅:
Displaying progress of file upload in a ProgressBar with SSH.NET

关于c# - 使用 SSH.NET 在进度栏中显示文件下载进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442714/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com