'RAR file downloaded with C# becoming corrupt [closed]

I'm download a .rar file from a github release but the downloaded file is becoming corrupt. The file is fine it's just not working when I download it with code

this is my current code

WebClient wc = new WebClient();
Uri fileLink = new Uri("https://api.github.com/repos/{my name}/{my repo}/releases/latest");
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(UpdateCompleted);
wc.DownloadFileAsync(fileLink, $@"{appRoot}\updated.rar");


Solution 1:[1]

My best guess is that the file that you want to download is not a .rar but some text (html or json) that contains information for the real download.

Can you open the downloaded file with notepad to see what it contains?

According to https://docs.github.com/en/rest/releases/releases#get-the-latest-release, it should contains a json file with some assets that contain the real download link.

Solution 2:[2]

You need to keep the WebClient instance alive during the download. What does that mean?

If the WebClient instance is only hold in the wc variable, and wc is being local to whatever method which contains the code in your question, then as soon as this method returns, the wc variable gets out of scope.

If there is no other field/property/whatever holding a reference to the WebClient instance, the WebClient instance becomes subject to the next garbage collection.

Normally, you cannot predict when a garbage collection happens (well, you can, but it's a complicated topic for another day...). If it so happens that the garbage collection happens while the download is still in progress, well, the WebClient instance will be finalized and destroyed anyway, effectively aborting download and leaving you with a partially downloaded file.

Another possibility is that your program just exits without actually waiting for the download to complete before exiting, again leaving you with a partially downloaded file.

Are there other possibilities of what might have gone wrong there with your download? Sure, there are. But for me, it's enough tea leaves reading for a day, so i leave the troubleshooting and debugging of your program and inspection of the downloaded data to you...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2