简体   繁体   English

WinSCP更新C#进度栏

[英]WinSCP update C# progress bar

I am having issues in knowing the percentage of a file sent at any point the FileTransferProgressEventArgs event is fired using WinSCP .NET assembly in C#. 我在知道在C#中使用WinSCP .NET程序集触发FileTransferProgressEventArgs事件在任何时候发送的文件的百分比时遇到问题。

The FileProgress returns only 0 or 1 , or the documentation said (0-1) which I don't understand. FileProgress仅返回01 ,或者文档说(0-1),我听不懂。 I need to know how much bytes of the file is sent but not 0,1 which i don't understand. 我需要知道发送了多少字节的文件,但没有0,1,我不明白。 I know CPS is the bytes per second but i need more variables. 我知道CPS是每秒的字节数,但是我需要更多的变量。

The method where i increment the progress bar is as below void 我增加进度条的方法如下空

SessionFileTransferProgress(object sender, FileTransferProgressEventArgs e)
{
    progressBar.Increment((int)e.FileProgress);
} 

Its e.FileProgress and e.CPS which I thought could help but seems I'm missing something. 我以为它的e.FileProgresse.CPS可以提供帮助,但似乎我缺少了一些东西。

The problem is, that you cast e.FileProgress to int . 问题是,您将e.FileProgressint As you allready said, the documentation specifies that the value of e.FileProgress ranges from 0 to 1, for example 0.55. 就像您已经说过的那样,文档指定e.FileProgress的值范围是0到1,例如0.55。 If you cast this value to an integer, you will lose all decimal places. 如果将此值转换为整数,则将丢失所有小数位。 So the resulting value will be 0. To solve this problem you can transform e.FileProgress into a percentage value by multiplying it with 100. So you get values ranging from 0 to 100. So you can implement your event handler like so: 因此,结果值将为0。要解决此问题,您可以将e.FileProgress乘以100来转换为百分比值。这样您就可以得到介于0到100之间的值。因此,您可以像这样实现事件处理程序:

void SessionFileTransferProgress(object sender, FileTransferProgressEventArgs e) 
{
    progressBar.Value = (int)(e.FileProgress * 100);
}

You just have to make sure, that the Minimum and Maximum properties of your progress bar are set to their default values 0 and 100. 您只需要确保将进度栏的“ Minimum和“ Maximum属性设置为其默认值0和100。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM