我在C#控制台应用程序中显示进度条。它正在做一些小错误。控制台应用程序中的进度条问题
这里的进度条代码:
private static void ProgressBar(int progress, int total)
{
//draw empty progress bar
Console.CursorLeft = 0;
Console.Write("["); //start
Console.CursorLeft = 32;
Console.Write("]"); //end
Console.CursorLeft = 1;
float onechunk = 30.0f/total;
//draw filled part
int position = 1;
for (int i = 0; i < onechunk * progress; i++)
{
Console.BackgroundColor = ConsoleColor.Green;
Console.CursorLeft = position++;
Console.Write(" ");
}
//draw unfilled part
for (int i = position; i <= 31; i++)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.CursorLeft = position++;
Console.Write(" ");
}
//draw totals
Console.CursorLeft = 35;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(progress.ToString() + " of " + total.ToString() + " ");
}
如果处理共5个文件会显示:
即使正确地处理了所有5文件。
例如,我将XML文件从目录加载到字符串数组中。
string[] xmlFilePath = Directory.GetFiles(xmlFullpath, "*.xml");
然后,我有一个for loop
和它里面我把我的进度条功能。
for (int i = 0; i < xmlFilePath.Length; i++)
{
ProgressBar(i, xmlFilePath.Length);
}
这就是我的工作方式。我知道,因为它从0开始打印0 1 2 3 4 out of 5。 但是我想从5中的第一个开始打印,第5个中的第2个....第5个中的5个。
所以我改变了我的for循环的位置是1
for (int i = 1; i< xmlFilePath.Length; i++)
{
}
开始在这种情况下,将只处理4个文件,所以我改变xmlFilePath.Length
到xmlFilePath.Length +1
,但我发现index out bound
例外。
关于如何解决这个问题的任何建议?
我为什么没有想到这个?! :) 谢谢。有效。 – smr5 2014-10-01 18:26:31