控制台应用程序中的进度栏

smr5:

我正在编写一个简单的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
}

输出仅为1943年的[] 0

我在这里做错了什么?

编辑:

在加载和导出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循环...有什么建议吗?

eddie_cat:

这行是你的问题:

drawTextProgressBar(0, totalCount);

您说的是每次迭代的进度均为零,应该将其递增。也许使用for循环代替。

for (int i = 0; i < filePath.length; i++)
{
    string FileName = Path.GetFileName(filePath[i]);
    //copy the files
    oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory + "/" + FileName);
    //Console.WriteLine("Uploading file..." + FileName);
    drawTextProgressBar(i, totalCount);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章