C# 如何显示ByTestTotal/Filesize的进度百分比,给出以下代码
下面的代码将文件上传到FTP服务器,变量numBytes总是接收512字节的块。ByTestTotal是这512个字节的总和。因此百分比将计算512/总数,1024/总数,等等。。。这很好,但它会显示太多的行 我希望Console.WriteLine最多显示10行,显示:10%,20%。。。90%, 100%. 我怎样才能做到这一点?这是我的密码:C# 如何显示ByTestTotal/Filesize的进度百分比,给出以下代码,c#,percentage,C#,Percentage,下面的代码将文件上传到FTP服务器,变量numBytes总是接收512字节的块。ByTestTotal是这512个字节的总和。因此百分比将计算512/总数,1024/总数,等等。。。这很好,但它会显示太多的行 我希望Console.WriteLine最多显示10行,显示:10%,20%。。。90%, 100%. 我怎样才能做到这一点?这是我的密码: int percentComplete = 0; long numBytes = ftp.DoUpload(); while (numBytes
int percentComplete = 0;
long numBytes = ftp.DoUpload();
while (numBytes > 0)
{
percentComplete = (int)Math.Round((double)(100 * ftp.BytesTotal) / ftp.FileSize);
Console.WriteLine("Bytes Transfered: " + percentComplete.ToString() + "%");
numBytes = ftp.DoUpload();
}
ftp.Disconnect();
更新代码:
private string PrintPercent2(long currentValue, long maxValue)
{
string percentProgress = "0%";
long oneTenth = (long)maxValue / 10;
long[] percentIntervals = new long[10];
for (int i = 0; i < percentIntervals.Length; i++) {
percentIntervals[i] = oneTenth * (i + 1);
}
if (currentValue > percentIntervals[0] && currentValue < percentIntervals[1])
percentProgress = "10%";
else if (currentValue > percentIntervals[1] && currentValue < percentIntervals[2])
percentProgress = "20%";
else if (currentValue > percentIntervals[2] && currentValue < percentIntervals[3])
percentProgress = "30%";
else if (currentValue > percentIntervals[3] && currentValue < percentIntervals[4])
percentProgress = "40%";
else if (currentValue > percentIntervals[4] && currentValue < percentIntervals[5])
percentProgress = "50%";
else if (currentValue > percentIntervals[5] && currentValue < percentIntervals[6])
percentProgress = "60%";
else if (currentValue > percentIntervals[6] && currentValue < percentIntervals[7])
percentProgress = "70%";
else if (currentValue > percentIntervals[7] && currentValue < percentIntervals[8])
percentProgress = "80%";
else if (currentValue > percentIntervals[8] && currentValue < percentIntervals[9])
percentProgress = "90%";
else if (currentValue >= maxValue)
percentProgress = "100%";
return percentProgress;
}
私有字符串PrintPercent2(长currentValue、长maxValue)
{
字符串percentProgress=“0%”;
long oneTenth=(long)maxValue/10;
长[]百分比间隔=新长[10];
对于(int i=0;iPercentInterval[0]&¤tValuePercentInterval[1]&¤tValuePercentInterval[2]&¤tValuePercentInterval[3]&¤tValuePercentInterval[4]&¤tValuePercentInterval[5]&¤tValuePercentInterval[6]&¤tValuePercentInterval[7]&¤tValuePercentInterval[8]&¤tValue=maxValue)
percentProgress=“100%”;
返回进度;
}
Jusy add if((完成百分比%10)==0)在您的WriteLine之前我编写了以下程序来帮助您。这有点粗糙,但它适用于我执行的每个测试:
static void Main(string[] args)
{
var rnd = new Random();
string lastValue = "";
long currentValue = 0;
long maxValue = LongRandom(0, 1134984, rnd);
while (currentValue <= maxValue)
{
lastValue = PrintProgress(currentValue, maxValue, lastValue);
currentValue++;
}
}
private static long LongRandom(long min, long max, Random rand)
{
var buf = new byte[8];
rand.NextBytes(buf);
long longRand = BitConverter.ToInt64(buf, 0);
return (Math.Abs(longRand % (max - min)) + min);
}
private static string PrintProgress(long currentValue, long maxValue, string lastValue)
{
if (currentValue == 0)
return "";
decimal q = decimal.Multiply((decimal.Divide(currentValue, maxValue) - 1), 100);
if (Math.Round(q, 0)%10 == 0)
{
string s = string.Format("{0}%", (Math.Abs(decimal.Subtract(Math.Abs(q), 100))).ToString("N0"));
if (s == lastValue)
return lastValue;
if (s == "0%")
return "0%";
Console.WriteLine(s);
return s;
}
return lastValue;
}
static void Main(字符串[]args)
{
var rnd=新随机数();
字符串lastValue=“”;
长电流值=0;
long maxValue=LongRandom(01134984,rnd);
while(currentValue)这不会很好地工作。我得到的结果是20,70,90100%。我试图将总文件大小拆分为10,然后根据传输的字节块显示百分比。基本上,我想让用户看到10,20,30,40,50,60,70,80,90100%你真的想要“10,20,30…”,或者您想要10个值,即使它们是11、22.1、30.05等?我想要10个值,无论它们是什么。我尝试使用“if(percentComplete%10==0)”,但如果文件太大,函数将打印0%数百次:(我现在没有时间写代码,但是考虑到你有512字节的块和一个已知的总大小,你可以以1/10的间隔测试512的实际倍数。嘿,Jon,是的,我更新了代码,它可以工作,除了我仍然需要只显示一次10%,只显示一次20%等等。我只是建立了一个非常基本的日志ic:)我可以在每个间隔中添加一个布尔值,并在显示值后将其设置为true。这解决了我的问题。我相信有更好的方法可以做到这一点,但这是我知道的方法:-)