WebClient C # ending download before downloading

1

I have a question, I'm using WebClient in C # language, I have the following problem: I click on Download a file, and it does not start downloading, it says download is already finished ...

Code:

WebClient Web = new WebClient();
                string Info = Web.DownloadString("https://drive.google.com/uc?authuser=0&id=1_bujHdC26AyVeFUZc-5UbDV-g8UuGdXS&export=download");
                string VAtualizada = Info.Split('\n')[0];
                V1 = VAtualizada.Split('.')[0];
                V2 = VAtualizada.Split('.')[1];
                V3 = VAtualizada.Split('.')[2];
                metroLabel2.Text = "Versão mais recente: V" + VAtualizada;
                frmGerador.Versao = VAtualizada;
                Web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Web_DownloadProgress);
                Web.DownloadFileCompleted += new AsyncCompletedEventHandler(Web_DownloadCompleted);
                Web.DownloadFileAsync(new Uri(Info.Split('\n')[1]), Application.StartupPath + @"\Gerador de Deck V" + V1 + "." + V2 + "." + V3 + " - Clash Royale.exe");

DownloadFileCompleted event:

void Web_DownloadCompleted(object sender, AsyncCompletedEventArgs e)
        {
                MessageBox.Show("A nova versão foi baixada com sucesso." + Environment.NewLine + "O programa será reiniciado.", "Atualização", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

Can anyone tell me why the error? And also in the DownloadProgress event I can only get the following information: e.BytesReceived

Code:

void Web_DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
        {
            metroLabel5.Text = "Baixado: " + e.BytesReceived / 1024 + "KB";
        }

Summarizing my doubts so far: - When you click Download, the file says: "New version downloaded successfully." without downloading. (Before it was normal, not now) - I can not use e.TotalBytesToReceive and e.ProgressPorcentage properties in the DownloadProgress method

Can anyone help me? Comment here below any questions!

    
asked by anonymous 22.12.2017 / 01:47

1 answer

0

Probably because the DownloadFileAsync method is suppressing some error, your split is not correctly breaking the string return and the download url is in the third position of the array, I made some changes and added the using because you were not doing the WebClient dispose as well.

Next comes the other detail, DownloadFileAsync is an asynchronous method so it does not block the thread and you have to wait for the end of its execution.

using (WebClient Web = new WebClient())
{        
    var taskNotifier = new AutoResetEvent(false);        

    string Info = Web.DownloadString("https://drive.google.com/uc?authuser=0&id=1_bujHdC26AyVeFUZc-5UbDV-g8UuGdXS&export=download");

    //Split pela quebra de linha \r\n
    string[] data = Info.Split(Environment.NewLine.ToCharArray());
    string VAtualizada = data[0];

    //Se não houver valor na segunda posição ele pega a da terceira
    //Adicione o tratamento que achar mais adequado;
    string url = string.IsNullOrEmpty(data[1]) ? data[1] : data[2];

    V1 = VAtualizada.Split('.')[0];
    V2 = VAtualizada.Split('.')[1];
    V3 = VAtualizada.Split('.')[2];
    metroLabel2.Text = "Versão mais recente: V" + VAtualizada;
    frmGerador.Versao = VAtualizada;

    Web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Web_DownloadProgress);
    Web.DownloadFileCompleted += new AsyncCompletedEventHandler(Web_DownloadCompleted);

    //Bloco try/catch para a captura e interrupção no caso de algum erro
    try
    {
        Web.DownloadFileAsync(new Uri(url), Application.StartupPath + @"\Gerador de Deck V" + V1 + "." + V2 + "." + V3 + " - Clash Royale.exe");
    }
    catch (Exception e)
    {
        throw e;
    }        

    taskNotifier.WaitOne();

}
    
22.12.2017 / 13:46