C# 控制台进度条

C# 控制台进度条,c#,console,progress,C#,Console,Progress,您好,所以当从FTP下载文件时,我需要在C控制台应用程序中设置进度条,但这对我来说有点太难了,所以我使用了已经完成的API和库 Im使用FluentFTP: 和progressbar控制台功能来自: 我做过这样的事情: //Download files from FTP, return true or false if succed public static void DownloadFileFromFTP(string ip, string RemoteFi

您好,所以当从FTP下载文件时,我需要在C控制台应用程序中设置进度条,但这对我来说有点太难了,所以我使用了已经完成的API和库

Im使用FluentFTP: 和progressbar控制台功能来自:

我做过这样的事情:


        //Download files from FTP, return true or false if succed
        public static void DownloadFileFromFTP(string ip, string RemoteFilePath, string LocalFilePath, string username, string password)
        {

             FtpClient client = new FtpClient(ip);
             client.Credentials = new NetworkCredential(username, password);
             client.Connect();

            using (var progress = new ProgressBar())
            {

                   client.DownloadFile(LocalFilePath, RemoteFilePath, FtpLocalExists.Overwrite, FluentFTP.FtpVerify.Retry, progress);

            }

        }
功能完全没有问题,一切正常。问题是progressbar的速度达到了100%,我并没有下载5%的文件,progressbar显示了100%

我是否有错误的文档,或者有人能帮我解决问题吗

谢谢


John

您所做的是在for循环上迭代100次,然后下载它的确切次数。您需要做的是创建一个进度条,将最小值设置为0,最大值设置为100。然后,您需要定义一个回调函数,该函数在根据文档进行调用时更新进度,这是由提供的函数完成的

为了保持简短,您需要添加从链接的文档中复制的以下内容

你的方法应该是这样的

        //Download files from FTP, return true or false if succed
    public static void DownloadFileFromFTP(string ip, string RemoteFilePath, string LocalFilePath, string username, string password)
    {
         ProgressBar progressBar = new ProgressBar();

         Progress<double> progress = new Progress<double>(x => {
           if (x > 0)
           {
             progressBar.Report((double) x / 100);
           }   
         });
         FtpClient client = new FtpClient(ip);
         client.Credentials = new NetworkCredential(username, password);
         client.Connect();
         progressBar = new ProgressBar();
         client.DownloadFile(LocalFilePath, RemoteFilePath, FtpLocalExists.Overwrite, FluentFTP.FtpVerify.Retry, progress);
         progressBar.Dispose();
    }

你确定要下载同一个文件101次吗?Sry旧代码,我编辑过。但它在没有for cycle的情况下也能做同样的事情。是的,我知道我只是复制了用deleted for cycle编辑的旧代码。但我发现了这个,但我认为它只适用于windows窗体,因为我不知道progressBar,我认为它是在windows窗体中创建的对象,我需要在控制台中使用它。我发布的函数没有像IsIndestime、Value这样的函数,直到在``ProgressProgress=new Progressx=>{if x>0{progressBar.Reportdoublex/100;}中使用未分配的局部变量'progressBar'```好的,然后尝试在第一行实例化进度条,然后ProgressBar ProgressBar应更改为ProgressBar ProgressBar=new ProgressBar;