I'm trying to download a file from the internet. I have an if statement and the else clause triggers the file download. This is my code:
if (!FileDownload)
{
}
else
{
using (WebClient wc = new WebClient())
{
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
wc.DownloadFileAsync(new Uri("https://www.google.com/Program.exe"),Application.StartupPath);
MessageBox.Show("Yes");
}
When I try to run my program I get the following errors:
Error 1:
Could not copy "obj\Debug\Program.exe" to "bin\Debug\Program.exe". Exceeded retry count of 10. Failed.
Error 2:
Unable to copy file "obj\Debug\Program.exe" to "bin\Debug\Program.exe". Could not find file 'obj\Debug\Program.exe'.
And around 10 warnings with the same message:
Could not copy "obj\Debug\Program.exe" to "bin\Debug\Program.exe". Beginning retry 1 in 1000ms. Could not find file 'obj\Debug\Program.exe'.
However, once I remove the else clause in my if statement , the program executes without any errors. The download code in the else clause is obviously causing the problem but I'm not sure why. I have used the same code in the same form at a different event and it's working flawlessly. Any help is appreciated.
I have already tried:
- Close and reopen visual studio
- Delete all the files from the bin folder then rebuild.
- Checked if the file is locked with Process Explorer
- Manually run VS as admin
- Delete bin and debug folder
- Reboot computer and move project to a different folder