加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用SSH.NET显示ProgressBar中文件上载的进度

发布时间:2020-12-15 23:38:52 所属栏目:百科 来源:网络整理
导读:我想在ProgressBar上显示上传过程的进度,这是我的按钮“上传”的代码: private void button2_Click(object sender,EventArgs e){ int Port = int.Parse(textBox2.Text); string Host = textBox1.Text; string Username = textBox3.Text; string Password =
我想在ProgressBar上显示上传过程的进度,这是我的按钮“上传”的代码:

private void button2_Click(object sender,EventArgs e)
{
    int Port = int.Parse(textBox2.Text);
    string Host = textBox1.Text;
    string Username = textBox3.Text;
    string Password = textBox4.Text;
    string WorkingDirectory = textBox6.Text;
    string UploadDirectory = textBox5.Text;

    FileInfo FI = new FileInfo(UploadDirectory);
    string UploadFile = FI.FullName;
    Console.WriteLine(FI.Name);
    Console.WriteLine("UploadFile" + UploadFile);

    var Client = new SftpClient(Host,Port,Username,Password);
    Client.Connect();
    if (Client.IsConnected)
    {
        var FS = new FileStream(UploadFile,FileMode.Open);
        if (FS != null)
        {
            Client.UploadFile(FS,WorkingDirectory + FI.Name,null);
            Client.Disconnect();
            Client.Dispose();
            MessageBox.Show(
                "Upload complete","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }
    }
}

解决方法

您必须提供对upload000allback参数的回调.

public void UploadFile(Stream input,string path,Action<ulong> uploadCallback = null)

当然,您必须在后台线程上传或使用异步上传(SftpClient.BeginUploadFile).

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

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

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

        using (var stream = new FileStream(SourcePath + FileName,FileMode.Open))
        using (var client = new SftpClient(Host,Password))
        {
            client.Connect();
            // Set progress bar maximum on foreground thread
            progressBar1.Invoke(
                (MethodInvoker)delegate { progressBar1.Maximum = (int)stream.Length; });
            // Upload with progress callback
            client.UploadFile(stream,RemotePath + FileName,UpdateProgresBar);
            MessageBox.Show("Upload complete");
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
}

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

enter image description here

下载请参阅:
Displaying progress of file download in a ProgressBar with SSH.NET

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读