Harry
Harry

Reputation: 321

Could not copy "obj\Debug\Program.exe" to "bin\Debug\Program.exe". Exceeded retry count of 10. Failed

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:

Upvotes: 4

Views: 6528

Answers (4)

Danial Roshanfekr
Danial Roshanfekr

Reputation: 21

I had the same error then I realized that I had run the project therefore after closing it and rebuilding again it was solved

Upvotes: -1

V D Paraliya
V D Paraliya

Reputation: 21

Just do one thing Open Visual Studio as Administrator Rights as show in below image. enter image description here

and problem solve do below trick to exit all process after you close your application so you dont get another problems like this.

Just try this one I think you will get your solution

Add Application.Exit() in Main form or MDI form's Closing event like below

Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Application.Exit() End Sub

Upvotes: 2

Harry
Harry

Reputation: 321

I solved the problem myself. After trying lots of different solutions that had worked for other people but not for me I tried to look at the different processes running at the same time I'm trying to execute the program. The problem was Avast. I turned off Avast Anti Virus and it instantly solved the problem. For some reason Avast didn't even show any messages every time it deleted the program. I have an existing problem with my program - in that some AV's (2/72) detect my program as a virus. It has something to do with my code structure. Thanks for the answers everyone - it should be useful to other people who have a similar error.

Upvotes: 0

vendettamit
vendettamit

Reputation: 14687

I'm sure you're not deleting the files from correct bin folder. Open the TaskManager look for any program named Program.exe. Verify where the process is running from Open file Location.

Kill the Program.exe and try rebuilding again. If it works then your bin output path is mapped to this location.

Just gave a close look at the error message: Unable to copy file "obj\Debug\Program.exe" to "bin\Debug\Program.exe". Could not find file 'obj\Debug\Program.exe'

Do you have any PostBuild step configured to copy files? if yes remove that.

Also edit your .csproj file and search for 'obj\Debug' if there's anything that matches with it delete that and try again.

More advanced troubleshooting:

Use the process monitor and filter the process for VisualStudio and the path. see below screenshot

enter image description here

Build your project again and Investigate the profiling logs why it's trying to copy the Program.exe.

enter image description here

Add/Remove more filters to see why it's tyring to copy the Program.exe. Also look for any Errors in Result column.

Upvotes: 0

Related Questions