4

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
Harry
  • 301
  • 1
  • 4
  • 9
  • 1
    You need to make sure the file isn't in use. – SLaks Dec 08 '16 at 16:49
  • 2
    Close and reopen visual studio – TheLethalCoder Dec 08 '16 at 16:50
  • Close the Project and open it back up. Try again. – Scott Dobbins Dec 08 '16 at 16:50
  • I assume the last successful build you has was before the download code was added – TheLethalCoder Dec 08 '16 at 16:50
  • @SLaks I already checked and it wasn't. – Harry Dec 08 '16 at 16:52
  • @Dobbins Also did that and didn't help. – Harry Dec 08 '16 at 16:53
  • @TheLethalCoder Yes, the last successful build was before the download code was added. But even if I remove the download code, the build starts working again. The moment I put the download code back in, I get an error again. – Harry Dec 08 '16 at 16:53
  • @Harry Because it is trying to copy the new codebase to the bin directory, when you remove the download code the codebase in the bin is the same and so it doesn't need to copy the files anymore. I get it quite regularly, you just have to close and reopen visual studio, essentially visual studio has a lock on its own files. – TheLethalCoder Dec 08 '16 at 16:54
  • @TheLethalCoder I did restart Visual studio and I even restarted my computer. None of them worked :/ – Harry Dec 08 '16 at 16:55
  • Delete all the files from the bin folder then rebuild. – Scott Dobbins Dec 08 '16 at 16:56
  • @Dobbins I also tried that. I even deleted both the debug and bin folders and it still didn't work. I tried most solutions that were posted in previous threads on stackoverflow and none of them worked. – Harry Dec 08 '16 at 16:58
  • Where is your download code located? – Scott Dobbins Dec 08 '16 at 16:59
  • Have you looked at the file with [Process Explorer](https://technet.microsoft.com/en-us/sysinternals/processexplorer.aspx)? That could help you find out what, if anything, has the file locked. – EJoshuaS - Stand with Ukraine Dec 08 '16 at 17:00
  • @Dobbins in the form load event. – Harry Dec 08 '16 at 17:00
  • 1
    @EJoshuaS Yeah I did and the file wasn't locked :/ – Harry Dec 08 '16 at 17:01
  • That's really odd.. I'd definitely like to see the answer to this one. Incidentally, it might be worth updating the question with the things that didn't work (e.g. the fact that Process Explorer didn't show anything locking the file, etc.) to make it more helpful to future readers too in case someone else encounters the same thing. – EJoshuaS - Stand with Ukraine Dec 08 '16 at 17:08
  • 1
    you need to close the application , right click on VS and select run as admin and this will correct the issue.. there is also some documentation on MSDN site on how to run this as admin at startup.. – MethodMan Dec 08 '16 at 17:08
  • @MethodMan Is this also necessary if the user's running the application outside of Visual Studio? – EJoshuaS - Stand with Ukraine Dec 08 '16 at 17:11
  • @MethodMan I have checked run as administrator in the Visual studio .exe properties and it shows the error. – Harry Dec 08 '16 at 17:14
  • @EJoshuaS Thanks , I edited my question. – Harry Dec 08 '16 at 17:17
  • try copying to a different folder then and seeing if it works can't hurt to try – MethodMan Dec 08 '16 at 17:22
  • reboot the machine and try again.. – MethodMan Dec 08 '16 at 17:23
  • @MethodMan he tried that already. No one has mentioned [this post](http://stackoverflow.com/questions/18102859/visual-studio-could-not-copy-during-build) yet, it indicates that the vshost process may be stuck in an exception, this might explain why him commenting code changes the behavior; that post also mentions about 100 other potential causes, really this post is probably a duplicate that one. The post also links [here](https://blog.ndepend.com/visual-studio-script-saves-time-pain/) which gives 6 bullet points of other potential causes, not all of which have been mentioned in this post yet. – Quantic Dec 08 '16 at 17:27
  • @MethodMan Already restarted my computer and copied the files to a different folder. Same errors. – Harry Dec 08 '16 at 17:27
  • @Quantic vshost.exe isn't even running for me and I already checked out that post and the solution didn't work for me unfortunately :/ – Harry Dec 08 '16 at 17:32
  • Read that post again, there isn't "the solution", yes there's an accepted answer but the rest of the answers are unrelated and different. E.g., "I have solved it by killing IISExpress in task manager", "i have the same problem in VS2015 update 2 - MSBuild,exe process needs to be killed in TaskManager before I can re-build", "Solution: Uninstalling the "Productivity Power Tools" in Visual Studio 2013.", and more. – Quantic Dec 08 '16 at 17:40

4 Answers4

2

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

0

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.

vendettamit
  • 14,315
  • 2
  • 32
  • 54
  • 1
    I already tried that and it didn't work. The problem is that the 'obj\debug\' folder doesn't build Program.exe. So it cannot be copied to the bin folder. I'm not sure why it's not working tho. – Harry Dec 08 '16 at 17:06
  • 1
    this is not the issue.. I have this problem all the time and figured out that running it as admin will correct the issue.. – MethodMan Dec 08 '16 at 17:09
  • My program already requires to be run as an admin (I have already edited the manifest file) and I have also ran Visual studio as an admin and the error still occurs. – Harry Dec 08 '16 at 17:13
  • open the task manager and stop all instances of that application / Visual Studio, reopen as admin, make sure in the header of VS that it says `Microsoft Visual Studio(Administrator)` and recompile the application if you have actual issues running this outside of debugger then perhaps you have some memory leaks / not releasing of resources properly.. – MethodMan Dec 08 '16 at 17:15
  • @MethodMan Thanks for the suggestion, I did this and it's still showing the same errors :/ – Harry Dec 08 '16 at 17:19
  • @vendettamit Thanks for your edited post. That was a new solution but unfortunately it didn't work. I don't have a postbuild and I couldn't find 'obj\Debug' in my cs project file. – Harry Dec 08 '16 at 17:34
  • See further steps for investigations. Hope this will give your source of error. – vendettamit Dec 08 '16 at 17:46
  • Thanks for the further edit. Unfortunately it didn't solve my problem but it might help others who have a similar error to mine. – Harry Dec 09 '16 at 23:35
0

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.

Community
  • 1
  • 1
Harry
  • 301
  • 1
  • 4
  • 9
-1

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