Webclient C# finishing download before starting to download

Asked

Viewed 279 times

1

I have a question, I am using Webclient in C#language, I have the following problem: I click on Download a file, and it does not start downloading, it says that downloading 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");

Event Downloadfilecompleted:

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 of 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: - By clicking Download, the file says: "New version successfully downloaded." without having downloaded. (Before it was normal, now not) - I cannot use the e.Totalbytestoreceive Properties or.Progresspercentageage in the Downloadprogress method

Can anyone help me? Comment below on any questions!

1 answer

1


Probably because the Downloadfileasync method is suppressing some error, its split is not properly breaking the string return and the download url is in the third position of the array, I made some changes and added using because you weren’t doing Webclient Dispose as well.

After comes the other detail, Downloadfileasync is an asynchronous method so it does not block the thread and you need 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();

}
  • Hello friend! Thanks for the help, I managed to solve! I will explain what was happening. It was all right to download, all right. The problem that occurred that I clicked on downloading, and already said "Downloaded successfully" without having downloaded, was why variables V1, V2 and V3 were strings, was I switch to whole type, which started to download! Already the error that I could not use the properties e.Totalbytestoreceive and neither e.Progresspercentageage, was why I was downloading files through Google Drive (??), I just switched to Mediafire, which worked all right! Tips, comment here;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.