我正在编写一个简单的C#控制台应用程序,用于将文件上传到SFTP服务器。然而,文件数量很大。我想要显示已上传的文件数量与待上传总文件数之比,或者仅显示已上传的文件数量。
首先,我获取所有文件和文件的总数。
string[] filePath = Directory.GetFiles(path, "*");
totalCount = filePath.Length;
然后我使用foreach循环逐个上传文件。
foreach(string file in filePath)
{
string FileName = Path.GetFileName(file);
//copy the files
oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory + "/" + FileName);
//Console.WriteLine("Uploading file..." + FileName);
drawTextProgressBar(0, totalCount);
}
在foreach循环中,我有一个进度条,但它存在问题,无法正确显示。private static void drawTextProgressBar(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.Gray;
Console.CursorLeft = position++;
Console.Write(" ");
}
//draw unfilled part
for (int i = position; i <= 31 ; i++)
{
Console.BackgroundColor = ConsoleColor.Green;
Console.CursorLeft = position++;
Console.Write(" ");
}
//draw totals
Console.CursorLeft = 35;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(progress.ToString() + " of " + total.ToString() + " "); //blanks at the end remove any excess
}
输出只是[ ] 0/1943
我在这里做错了什么?
编辑:
我试图在加载和导出XML文件时显示进度条。 但它正在经过一个循环。 它完成第一轮后,就会进入第二轮,依此类推。
string[] xmlFilePath = Directory.GetFiles(xmlFullpath, "*.xml");
Console.WriteLine("Loading XML files...");
foreach (string file in xmlFilePath)
{
for (int i = 0; i < xmlFilePath.Length; i++)
{
//ExportXml(file, styleSheet);
drawTextProgressBar(i, xmlCount);
count++;
}
}
它从未离开for循环...有任何建议吗?
ExportXml(xmlFilePath[i])
。 - eddie_cat