0

I'm with something new, I'm blocked and I don't know how to do the truth.

I am making a program that downloads by FTP to do a series of steps, the first thing is to download.

Here I have the code that makes the download which works perfect:

public static void DescargarFichero(string ficFTP, string user, string pass, string dirLocal, Boolean UsePassive, Boolean UseBinary)
{
    FtpWebRequest dirFtp = ((FtpWebRequest)FtpWebRequest.Create(ficFTP));
    dirFtp.KeepAlive = true;
    dirFtp.UsePassive = UsePassive;
    dirFtp.UseBinary = UseBinary;            
    // Los datos del usuario (credenciales)
    NetworkCredential cr = new NetworkCredential(user, pass);
    dirFtp.Credentials = cr;

    FtpWebResponse response = (FtpWebResponse)dirFtp.GetResponse();          
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);           

    using (FileStream writer = new FileStream(dirLocal, FileMode.Create))
    {
        long length = response.ContentLength;
        int bufferSize = 2048;
        int readCount;
        byte[] buffer = new byte[2048];

        readCount = responseStream.Read(buffer, 0, bufferSize);
        while (readCount > 0)
        {
            writer.Write(buffer, 0, readCount);
            readCount = responseStream.Read(buffer, 0, bufferSize);
            Console.WriteLine("Descargando...");
        }
    }

    reader.Close();
    response.Close();
}

I am doing tests in a console application, but the future is to use windows form and that it looks good, my blocking is this: How can I show this to happen:

while (readCount > 0)
{
    writer.Write(buffer, 0, readCount);
    readCount = responseStream.Read(buffer, 0, bufferSize);
    Console.WriteLine("Descargando...");
}

I can find out that this function is executing and that it is "downloading" I am looking for that every time that cycle is iterating I can return a value without having to break the cycle. My idea is to be able to say If Value is = X then "downloading"

Newbie question? Yes, I am blocked and I don't know how to get out of the "rat race". I hope you can help me.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
jose angarita
  • 199
  • 1
  • 12
  • So I'm guessing you wish to show that the download is 'progressing'? In Winforms, you could look into using a Progress Bar, but at the moment I would simply use the Console.Write() as you are doing. – Hex Apr 07 '20 at 19:25
  • HI, this solution: https://stackoverflow.com/questions/45269263/ftpwebrequest-ftp-download-with-progressbar dont work for my. When the software is running and it gets to this point: int size = (int)sizeRequest.GetResponse().ContentLength; I receive an error 550, where it tells me that the file does not exist or cannot be accessed. The funny thing is that if I use FtpWebRequest if the file is downloaded but I can't get the size. – jose angarita Apr 14 '20 at 07:45

1 Answers1

-2

Showing progress percentage

 readCount = responseStream.Read(buffer, 0, bufferSize);
 int current = 0; //Create a new int variable to count iterations
 while (readCount > 0)
 {
     writer.Write(buffer, 0, readCount);
     readCount = responseStream.Read(buffer, 0, bufferSize);

     current++; //Increment the counter
     double percentProgress = (current/readCount) * 100; //Calculate the percent
     string percentString = percentProgress.ToString() + "%"; //Append a %
     Console.WriteLine(percentString); //Write to console

 }

See this post if you wish to keep it on one line: Can Console.Clear be used to only clear a line instead of whole console?

Hex
  • 135
  • 1
  • 12
  • Hi! thank u for you answer i had a idea same that this, but i have the same problem. Now i need that you imaginary how will be this case if dont exist the console?. I wanna say, this function is a class and in the future i want instance the class in other section of the aplication for example, i want show a progress bar. The theme is that while the cicle is itering i can't return any value because if i return any value the cicle, end. – jose angarita Apr 08 '20 at 06:30
  • In this case, what I had thought was to know the size of the file and calculate what I was downloading, of course the problem is how do I get that function to return the value without breaking the cycle? Imagine something more basic, how do I return the text that says "Downloading" while the cycle works as I return that text? – jose angarita Apr 08 '20 at 06:32
  • `current/readCount` definitely does not calculate progress. For that you need first find out file size and then use sum of `readCount`. For GUI progress, see [FtpWebRequest FTP download with ProgressBar](https://stackoverflow.com/q/45269263/850848). – Martin Prikryl Apr 08 '20 at 06:33