199

Update: A sample project reproducing this bug can be found here at Microsoft Connect. I have also tested and verified that the solution given in the accepted answer below works on that sample project. If this solution doesn't work for you, you are probably having a different issue (which belongs in a separate question).


This is a question asked before, both here on Stack Overflow and other places, but none of the suggestions I've found this far have helped me, so I just have to try asking a new question.

Scenario: I have a simple Windows Forms application (C#, .NET 4.0, Visual Studio 2010). It has a couple of base forms that most other forms inherit from, it uses Entity Framework (and POCO classes) for database access. Nothing fancy, no multi-threading or anything.

Problem: All was fine for a while. Then, all out of the blue, Visual Studio failed to build when I was about to launch the application. I got the warning "Unable to delete file '...bin\Debug\[ProjectName].exe'. Access to the path '...bin\Debug\[ProjectName].exe' is denied." and the error "Unable to copy file 'obj\x86\Debug\[ProjectName].exe' to 'bin\Debug\[ProjectName].exe'. The process cannot access the file 'bin\Debug\[ProjectName].exe' because it is being used by another process." (I get both the warning and the error when running Rebuild, but only the error when running Build - don't think that is relevant?)

I understand perfectly fine what the warning and error message says: Visual Studio is obviously trying to overwrite the exe-file while it at the same time has a lock on it for some reason. However, this doesn't help me find a solution to the problem... The only thing I've found working is to shut down Visual Studio and start it again. Building and launching then work, until I make a change in some of the forms, then I have the same problem again and have to restart... Quite frustrating!

As I mentioned above, this seems to be a known problem, so there are lots of suggested solutions. I'll just list what I've already tried here, so people know what to skip:

  • Creating a new clean solution and just copy the files from the old solution.

  • Adding the following to the following to the project's pre-build event:

     if exist "$(TargetPath).locked" del "$(TargetPath).locked"
        if not exist "$(TargetPath).locked" if exist "$(TargetPath)" move "$(TargetPath)" "$(TargetPath).locked"
    
  • Adding the following to the project properties (.csproj file):

     <GenerateResourceNeverLockTypeAssemblies>true</GenerateResourceNeverLockTypeAssemblies>
    

However, none of them worked for me, so you can probably see why I'm starting to get a bit frustrated. I don't know where else to look, so I hope somebody has something to give me! Is this a bug in VS, and if so is there a patch? Or has I done something wrong, do I have a circular reference or similar, and if so how could I find out?

Any suggestions are highly appreciated :)

Update: As mentioned in the comment below, I've also checked using Process Explorer that it actually is Visual Studio that is locking the file.

sajadre
  • 1,141
  • 2
  • 15
  • 30
Julian
  • 20,008
  • 17
  • 77
  • 108
  • 4
    Have you checked if your application closes properly ? Does task manager show you [ProjectName].exe in list of processes ? – miensol May 24 '10 at 09:25
  • 2
    I've had this before and I simply renamed the file to .old etc and re ran the build. Not exactly a fix I know, but it worked for me. – codingbadger May 24 '10 at 09:36
  • @miensol: Yes, it seems to close properly. I get "The program '[1848] [ProjectName].vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0)." @Barry: renaming the exe-file in bin\Debug works, but as you said it's not really a solution and will be quite annoying to have to do every time. A bit better than restarting Visual Studio though... – Julian May 24 '10 at 09:48
  • Does this happen on a clean Windows Forms solution as well? New project -> Windows Forms -> Build, run, shutdown, rebuild? – Patrick May 24 '10 at 09:59
  • @Patrick: no, it does not happen on a clean solution. – Julian May 24 '10 at 10:04
  • 2
    @Naliluj: I came across [this](http://connect.microsoft.com/VisualStudio/feedback/details/114694/msb3021-unable-to-copy-file-bug-still-not-fixed-in-final-version#) article from a Microsoft forum that explains that it can be related to resource files. If you are using resx files this could give a hint. – Patrick May 24 '10 at 10:23
  • @Patrick: I've already ran into that article several times. I do only have the default resx-file (and it is even empty). Also, the workarounds suggested by the article is the "if exist $(TargetPath).locked" and the "GenerateResourceNeverLockTypeAssemblies". Both are tried, and none of them worked :( – Julian May 24 '10 at 10:29
  • @fearofawhackplanet: No, it is not (and just to rule out the option 100%, I even emptied it but that didn't make it any better)... – Julian May 24 '10 at 11:38
  • 1
    For posterity, I had this problem and it was solved by adding the true element to my csproj file. – ThisIsTheDave Jun 05 '10 at 03:10
  • @ThisIsDave: as you can see from my question, this is one of the suggested solutions I found when searching, but it didn't work in my case. – Julian Jun 05 '10 at 12:03
  • Thanks for the heads up! I've protected the question and removed the 'me too' answers. – Tim Post Feb 24 '11 at 13:14
  • This solution did not work for me. Tried changing ether and both AssemblyVersion and AssemblyFileVersion. – Johan Nov 21 '12 at 12:49
  • Something probably closely related (same MSBuild messages) in the SharpDevelop forums: [thread](http://community.sharpdevelop.net/forums/t/19968.aspx) – O. R. Mapper Jan 18 '14 at 11:09
  • It's being used by another process. Sometimes, even if you close the project in Visual Studio, it still has a lock on the file. Exiting Visual Studio (and anything else that uses these files) should help. – jay_t55 Sep 21 '14 at 09:44
  • pre-build even worked out perfectly, thanks – YazX Sep 28 '15 at 11:46

36 Answers36

120

This is going to sound stupid, but I tried all these solutions, running VS2010 on Windows 7. None of them worked except the renaming and building, which was VERY tedious to say the least. Eventually, I tracked down the culprit, and I find it hard to believe. But I was using the following code in AssemblyInfo.cs...

[assembly: AssemblyVersion("2.0.*")]

This is pretty common, but for some reason, changing the version to 2.0.0.0 made things work again. I don't know if it's a Windows 7 specific thing (I've only been using it for 3-4 weeks), or if it's random, or what, but it fixed it for me. I'm guessing that VS was keeping a handle on each file it generated, so it would know how to increment things? I'm really not sure and have never seen this happen before. But if someone else out there is also pulling their hair out, give it a try.

drharris
  • 11,194
  • 5
  • 43
  • 56
  • 12
    That is one crazy idea, I'll give you that ;) What's even crazier, is that it actually seems to work! I have tested this several times now, and I can confirm that when using an assembly version such as "2.0.*" I get the error, but when I instead use "2.0.0" it works as a charm! I urge more people to test this, and if you find it working please vote up this answer, because this is something that needs to be known! Hope Microsoft picks up on this... Thank you drharris :) – Julian Jun 08 '10 at 07:44
  • I'm so glad it worked for you too! I still have no idea what could be causing this to happen. I made file and folder permissions wide open, removed as many .resx files as I could, used pre and post build actions, put that Lock line into the project file, created a whole new solution, and reverted to the previous version in subversion. None of it worked, and I just happened to try this, to put it back in as much a "default" state as possible. Sure enough, it worked, but I have no clue why. – drharris Jun 08 '10 at 14:36
  • 2
    this did not work for me, when I restarting VS I did not get error for sometime. Every time I get this error I have to restart VS 2010 – Sharique Jan 20 '11 at 06:03
  • Did you ensure that both AssemblyVersion and AssemblyFileVersion were set like this? Also, if you just make this change, you should restart Visual Studio before trying again. It more than likely has a lock on those files, and won't reset it until the next time. Doing a clean build may help too. You basically need to force VS to release those file handles. – drharris Jan 20 '11 at 07:30
  • 6
    fyi...this did not work for me. My settings have always been: [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] – tbone Feb 16 '11 at 16:31
  • Argh! Such an annoying bug. Thanks for the fix. Has anyone reported this at MS side? Could we have a link on the follow-up? – Lazlo Mar 14 '11 at 21:44
  • I believe it was supposed to be addressed in SP1, but I don't have the guts to install it yet. Maybe someone can report back if they have installed it already. – drharris Mar 15 '11 at 02:47
  • 4
    If you currently have [assembly: AssemblyVersion("1.0.0.0")], replace it with [assembly: AssemblyVersion("2.0.0.0")] (i.e. '2' instead of the '1'). It worked for me. Although I haven't checked, it's possible that simply changing the version to anything other than what you've got now can fix this problem. – G S May 31 '11 at 12:32
  • 1
    Works for dll too! VS says cannot copy the dll and after changing **BOTH** [assembly: AssemblyVersion] and [assembly: AssemblyFileVersion()] from 1.0.* to 2.0.0.0, it worked. – AZ. Dec 09 '11 at 18:43
  • I had this same problem. I had three projects under one solution, an asp.net website project, a vb.net library project and C#.net library project. Website project build successfully but other project did not build and gave the same exception as above. I tried changing the AssemblyVersion but it did not work. What i tried was changing the readonly property. Removed readonly property and it worked. Now all the three projects in the solution build successfully. Now i again see the readonly property of the two project folder and i see they are readonly still, but it works. :) – NCCSBIM071 Jun 29 '12 at 07:35
  • 1
    Even better: this isn't just the auto-increment. I can't change `AssemblyVersion`, whether with my own program, Notepad++, or even VS itself. On the other hand, it seems that only changes to `AssemblyVersion` trigger it. `AssemblyFileVersion` and `AssemblyInformationalVersion` can be freely changed. – Bob Jul 03 '13 at 04:25
  • unbelievable!!! it seems changing to a version other than the current one fixes the problem, thanx – sawe Nov 13 '13 at 15:35
  • 1
    If this works for you (as it did for me) I later found that it was because the copy command was not on the last project built. If you put the copy command on the last project (right click solution > build order) that will also correct this problem. – John S. Jun 02 '14 at 18:12
  • And here we are in 2015 and in VS2015 and the problem still occurs and the solution is still the same. – Richard Nov 24 '15 at 08:22
  • The solution may work, but you lose sane version information in your files as a result. [comment36988469](http://stackoverflow.com/questions/2895898/visual-studio-build-fails-unable-to-copy-exe-file-from-obj-debug-to-bin-debug#comment36988469_2994502) looks closer to the root cause. – ivan_pozdeev Jan 06 '16 at 03:28
  • 1
    I ran into the same problem using VS 2015 Enterprise with Update 1 on WIndows 8.1. I fixed the Problem on @Nailuj advice, but I want to point out that in my AsseblyInfo it said [assembly: AssemblyVersion("1.0.0.0")] and to make it work I changed it to [assembly: AssemblyVersion("1.0.0")] – fose Feb 06 '16 at 09:29
14

I found one simple solution, just disable the Windows Indexing Services for the project folder and subfolders

Julian
  • 20,008
  • 17
  • 77
  • 108
Pedro
  • 2,907
  • 6
  • 34
  • 46
  • 2
    This worked for me as well. I'm not sure that I understand why, as process explorer showed devenv.exe was holding the locking handle. Nevertheless, turning off indexing fixed the problem. – Fopedush Jun 24 '14 at 18:53
  • 1
    @Fopedush I encountered this problem with the same solution, although I didn't see this question at the time. [This answer](http://stackoverflow.com/a/14573201/1229237) has some explanation as to why it helps. – DJH Sep 25 '14 at 08:59
  • 1
    This one did it for me. – Martin Capodici Oct 26 '14 at 02:17
14

Since I haven't gotten any more feedback on this issue, I thought I'd just share what ended up being my solution:

As suggested by Barry in a comment to the original post, manually renaming the '...bin\Debug[ProjectName].exe' to something else (e.g. '[ProjectName]1.exe') is one work-around (I'm however not allowed to delete the file myself, and I must say I find that a bit weird as one would believe the same lock preventing deletion would also prevent renaming...). It's not a good solution, but it's reasonable fast (at least after you've done it a couple of times, it almost becomes a routine), and at least way faster than restarting Visual Studio which is what I did in the beginning.

In case somebody wonders, I could also add that I only see this problem semi-randomly. It usually happens after I've done some changes in the design mode of a form (but not always). It usually doesn't happen if I only change business-logic code or non-visual related code (but sometimes it does...). Frustrating indeed, but at least I have a hack that works for me - let's just hope that my next project doesn't face this problem as well...

@Barry: if you would like to get credit for your comment, please feel free to post it as an answer and I'll make sure to accept it :)

Julian
  • 20,008
  • 17
  • 77
  • 108
  • I've voted this up as this is what I've done in the past. I agree, it's a dirty solution but it does work. VS has had this problem for a few iterations. I'm loading my project from a network dir as well. It's been fully trusted, but it doesn't matter. It doesn't matter if it a drive mapping or a UNC either. Yeah, MS really needs to fix this one. They have a closed bug for it saying unable to reproduce. Lame! – Josh Robinson Jun 07 '10 at 12:44
12

I have the same problem (MSB3021) with WPF project in VS2008 (on Windows 7 x32). The problem appearing if i try to re-run application too quick after previous run. After a few minutes exe-file unlocked by itself and i can re-run application again. But such a long pause angers me. The only thing that really helped me was running VS as Administrator.

Sergey
  • 301
  • 3
  • 10
  • 1
    I found this recent bug report about this exact issue: https://connect.microsoft.com/VisualStudio/feedback/details/558848/vsip-rebuilding-a-project-with-open-designers-twice-causes-an-error-with-locked-bin-debug-dlls That bug report provided a sample project that were able to reproduce the bug. The solution suggested by drharris worked there as well (see the workaround posted in the above link for a step-by-step solution to the sample project). – Julian Dec 15 '10 at 12:49
  • This is for sure easier than restarting VS. – Elvedin Hamzagic Jan 05 '15 at 15:32
  • 1
    "Page not found" for connect issue, did they just delete it out of embarrassment =S Was there ever a posted solution/workaround for this? – Paul C Jan 19 '15 at 16:40
9

When I have come across this problem it is to do with the Fact that the project I am trying to build is set as the Startup project in the solution making the .exe in the obj folder locked ( it also appears in your task manager,) right click another project in your solution and choose set startup project. This will release the lock, remove it from task manager and should let you build.

Adrian Booth
  • 154
  • 1
  • 6
  • 1
    This works every time for me. It seems to be to do with the vshost process that is generated and started for services – jaywayco May 31 '13 at 09:16
8

I tried all the other suggestions in the answers here, none of which worked. Eventually I used Process Monitor to discover that my .exe that VS2010 was failing to build was locked by the System process (PID=4). Searching SO for situations involving this yielded this answer.

Summarised: if you have the Application Experience service disabled (as I did) then re-enable and start it. Two years of aggravation ended.

Community
  • 1
  • 1
Eight-Bit Guru
  • 9,756
  • 2
  • 48
  • 62
  • +1 I previously tried everything else (1. task manager, 2. process explorer i.e. close handle which windows wouldn't let me do, 3. Disabling antivirus, 4. excluding APP_DATA/Local/Microsoft/Visual Studio from Windows Indexing service.) but this tip re: "Application Experience" service is the only one that saved me banging my head against the wall. I enabled it and the problem went away. Funny thing is, after I disabled it again everything was still OK. I haven't had any more problems. But definitely this is the only thing that sorted it for me. – Tomás Feb 20 '15 at 14:20
  • Work for me too!!! The only other thing that work also is delete the *bin* folder before run the application, but you must to delete always before run, very annoying. – E_Blue Mar 21 '16 at 01:36
6

I also had a problem very similar to this and found the reason in my case was that I had made the bin\debug folder available as a shared folder under VMware and either VMware, Explorer under the VM guest, or maybe even an anti-virus program under the guest (though I don't think I had one installed) was holding a handle to the file(s).

Julian
  • 20,008
  • 17
  • 77
  • 108
Quinxy von Besiex
  • 976
  • 11
  • 21
  • I have Avast installed and this morning I got a random MVC error saying that my dll had a virus in it. After the error, i could no longer build my MVC project. I added an exception to the Avast File System Shield and everything is working again. – Dusty Sep 26 '13 at 13:38
5

Disable "Enable the Visual Studio hosting process" Project Debug Settings

BCS Software
  • 1,745
  • 1
  • 11
  • 8
4

IF YOUR PROBLEM IS NOT SOLVED YET:

Visual studio's error is :

"The process cannot access the file 'bin\Debug**app.exe**' because it is being used by another process."

So ,go to task manager of windows(Ctrl+Shift+Esc),find your application name and force it to close by Endprocces.

AmirHossein Rezaei
  • 1,086
  • 1
  • 16
  • 20
4

I'd suggest download Process Explorer to find out exactly what process is locking the file. It can be found at:

http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • I agree--it is not necessarily VS that is locking the file. Virus checkers can be guilty of this. Try turning off your virus checker to see if that helps. – Polyfun May 24 '10 at 09:50
  • Sorry, I forgot to mention that I've already done that. And it says that is is Visual Studio (devenv.exe) that has a lock on the file ([ProjectName].vshost.exe). So that doesn't help me much either. – Julian May 24 '10 at 09:52
  • @ShellShock: disabling my anti virus (Avast) doesn't help either. – Julian May 24 '10 at 10:06
  • For me, using Sysinternals ProcessExplorer, I can see a handle to that file, but when I click on it, no application is shown holding it, and when I try to close the handle, I get a "Error opening process: the handle is invalid." error in ProcessExplorer. Yet the lock still persists. – tbone Feb 16 '11 at 16:33
4

Using Visual Studio I could never come up with a simple project to reproduce the error.

My solution was to disable the Visual Studio Hosting process.

For those interested I have attached a handle trace for the offending handle:

0:044> !htrace 242C
--------------------------------------
Handle = 0x000000000000242c - OPEN
Thread ID = 0x0000000000001cd0, Process ID = 0x0000000000001a5c

0x000000007722040a: ntdll!ZwCreateFile+0x000000000000000a
0x0000000074b4bfe3: wow64!whNtCreateFile+0x000000000000010f
0x0000000074b3cf87: wow64!Wow64SystemServiceEx+0x00000000000000d7
0x0000000074ac276d: wow64cpu!TurboDispatchJumpAddressEnd+0x0000000000000024
0x0000000074b3d07e: wow64!RunCpuSimulation+0x000000000000000a
0x0000000074b3c549: wow64!Wow64LdrpInitialize+0x0000000000000429
0x00000000772184c8: ntdll!LdrpInitializeProcess+0x00000000000017e2
0x0000000077217623: ntdll! ?? ::FNODOBFM::`string'+0x000000000002bea0
0x000000007720308e: ntdll!LdrInitializeThunk+0x000000000000000e
0x00000000773d0066: ntdll_773b0000!NtCreateFile+0x0000000000000012
0x000000007541b616: KERNELBASE!CreateFileW+0x000000000000035e
0x0000000075b42345: KERNEL32!CreateFileWImplementation+0x0000000000000069
0x000000006a071b47: mscorwks_ntdef!StgIO::Open+0x000000000000028c
--------------------------------------
Handle = 0x000000000000242c - CLOSE
Thread ID = 0x0000000000000cd4, Process ID = 0x0000000000001a5c

0x000000007721ffaa: ntdll!ZwClose+0x000000000000000a
0x0000000074b3f2cd: wow64!whNtClose+0x0000000000000011
0x0000000074b3cf87: wow64!Wow64SystemServiceEx+0x00000000000000d7
0x0000000074ac276d: wow64cpu!TurboDispatchJumpAddressEnd+0x0000000000000024
0x0000000074b3d07e: wow64!RunCpuSimulation+0x000000000000000a
0x0000000074b3c549: wow64!Wow64LdrpInitialize+0x0000000000000429
0x000000007724d177: ntdll! ?? ::FNODOBFM::`string'+0x000000000002bfe4
0x000000007720308e: ntdll!LdrInitializeThunk+0x000000000000000e
0x00000000773cf992: ntdll_773b0000!ZwClose+0x0000000000000012
0x0000000075b42642: KERNEL32!BaseRegCloseKeyInternal+0x0000000000000041
0x0000000075b425bc: KERNEL32!RegCloseKey+0x000000000000007d
*** WARNING: Unable to verify checksum for mscorlib.ni.dll
0x0000000068f13ca3: mscorlib_ni+0x0000000000233ca3
0x0000000069bc21db: mscorwks_ntdef!CallDescrWorker+0x0000000000000033
0x0000000069be4a2a: mscorwks_ntdef!CallDescrWorkerWithHandler+0x000000000000008e
--------------------------------------
Handle = 0x000000000000242c - OPEN
Thread ID = 0x00000000000006cc, Process ID = 0x0000000000001a5c

0x0000000077220e0a: ntdll!NtOpenKeyEx+0x000000000000000a
0x0000000074b5d1c9: wow64!Wow64NtOpenKey+0x0000000000000091
0x0000000074b5313b: wow64!whNtOpenKeyEx+0x0000000000000073
0x0000000074b3cf87: wow64!Wow64SystemServiceEx+0x00000000000000d7
0x0000000074ac276d: wow64cpu!TurboDispatchJumpAddressEnd+0x0000000000000024
0x0000000074b3d07e: wow64!RunCpuSimulation+0x000000000000000a
0x0000000074b3c549: wow64!Wow64LdrpInitialize+0x0000000000000429
0x000000007724d177: ntdll! ?? ::FNODOBFM::`string'+0x000000000002bfe4
0x000000007720308e: ntdll!LdrInitializeThunk+0x000000000000000e
0x00000000773d0fca: ntdll_773b0000!NtOpenKeyEx+0x0000000000000012
0x0000000075b42721: KERNEL32!LocalBaseRegOpenKey+0x000000000000010c
0x0000000075b428c9: KERNEL32!RegOpenKeyExInternalW+0x0000000000000130
0x0000000075b427b5: KERNEL32!RegOpenKeyExW+0x0000000000000021
--------------------------------------
Handle = 0x000000000000242c - CLOSE
Thread ID = 0x0000000000000cd4, Process ID = 0x0000000000001a5c

0x000000007721ffaa: ntdll!ZwClose+0x000000000000000a
0x0000000074b3f2cd: wow64!whNtClose+0x0000000000000011
0x0000000074b3cf87: wow64!Wow64SystemServiceEx+0x00000000000000d7
0x0000000074ac276d: wow64cpu!TurboDispatchJumpAddressEnd+0x0000000000000024
0x0000000074b3d07e: wow64!RunCpuSimulation+0x000000000000000a
0x0000000074b3c549: wow64!Wow64LdrpInitialize+0x0000000000000429
0x000000007724d177: ntdll! ?? ::FNODOBFM::`string'+0x000000000002bfe4
0x000000007720308e: ntdll!LdrInitializeThunk+0x000000000000000e
0x00000000773cf992: ntdll_773b0000!ZwClose+0x0000000000000012
0x0000000075b42642: KERNEL32!BaseRegCloseKeyInternal+0x0000000000000041
0x0000000075b425bc: KERNEL32!RegCloseKey+0x000000000000007d
0x0000000068f13ca3: mscorlib_ni+0x0000000000233ca3
0x0000000069bc21db: mscorwks_ntdef!CallDescrWorker+0x0000000000000033
0x0000000069be4a2a: mscorwks_ntdef!CallDescrWorkerWithHandler+0x000000000000008e
--------------------------------------
Handle = 0x000000000000242c - OPEN
Thread ID = 0x0000000000001cd0, Process ID = 0x0000000000001a5c

0x0000000077220e0a: ntdll!NtOpenKeyEx+0x000000000000000a
0x0000000074b5d1c9: wow64!Wow64NtOpenKey+0x0000000000000091
0x0000000074b5313b: wow64!whNtOpenKeyEx+0x0000000000000073
0x0000000074b3cf87: wow64!Wow64SystemServiceEx+0x00000000000000d7
0x0000000074ac276d: wow64cpu!TurboDispatchJumpAddressEnd+0x0000000000000024
0x0000000074b3d07e: wow64!RunCpuSimulation+0x000000000000000a
0x0000000074b3c549: wow64!Wow64LdrpInitialize+0x0000000000000429
0x00000000772184c8: ntdll!LdrpInitializeProcess+0x00000000000017e2
0x0000000077217623: ntdll! ?? ::FNODOBFM::`string'+0x000000000002bea0
0x000000007720308e: ntdll!LdrInitializeThunk+0x000000000000000e
0x00000000773d0fca: ntdll_773b0000!NtOpenKeyEx+0x0000000000000012
0x0000000075b42721: KERNEL32!LocalBaseRegOpenKey+0x000000000000010c
0x0000000075b428c9: KERNEL32!RegOpenKeyExInternalW+0x0000000000000130
--------------------------------------
Handle = 0x000000000000242c - CLOSE
Thread ID = 0x0000000000000cd4, Process ID = 0x0000000000001a5c

0x000000007721ffaa: ntdll!ZwClose+0x000000000000000a
0x0000000074b3f2cd: wow64!whNtClose+0x0000000000000011
0x0000000074b3cf87: wow64!Wow64SystemServiceEx+0x00000000000000d7
0x0000000074ac276d: wow64cpu!TurboDispatchJumpAddressEnd+0x0000000000000024
0x0000000074b3d07e: wow64!RunCpuSimulation+0x000000000000000a
0x0000000074b3c549: wow64!Wow64LdrpInitialize+0x0000000000000429
0x000000007724d177: ntdll! ?? ::FNODOBFM::`string'+0x000000000002bfe4
0x000000007720308e: ntdll!LdrInitializeThunk+0x000000000000000e
0x00000000773cf992: ntdll_773b0000!ZwClose+0x0000000000000012
0x0000000075b42642: KERNEL32!BaseRegCloseKeyInternal+0x0000000000000041
0x0000000075b425bc: KERNEL32!RegCloseKey+0x000000000000007d
0x0000000068f13ca3: mscorlib_ni+0x0000000000233ca3
0x0000000069bc21db: mscorwks_ntdef!CallDescrWorker+0x0000000000000033
0x0000000069be4a2a: mscorwks_ntdef!CallDescrWorkerWithHandler+0x000000000000008e
--------------------------------------
Handle = 0x000000000000242c - OPEN
Thread ID = 0x0000000000001cd0, Process ID = 0x0000000000001a5c

0x0000000077220e0a: ntdll!NtOpenKeyEx+0x000000000000000a
0x0000000074b5d1c9: wow64!Wow64NtOpenKey+0x0000000000000091
0x0000000074b5313b: wow64!whNtOpenKeyEx+0x0000000000000073
0x0000000074b3cf87: wow64!Wow64SystemServiceEx+0x00000000000000d7
0x0000000074ac276d: wow64cpu!TurboDispatchJumpAddressEnd+0x0000000000000024
0x0000000074b3d07e: wow64!RunCpuSimulation+0x000000000000000a
0x0000000074b3c549: wow64!Wow64LdrpInitialize+0x0000000000000429
0x00000000772184c8: ntdll!LdrpInitializeProcess+0x00000000000017e2
0x0000000077217623: ntdll! ?? ::FNODOBFM::`string'+0x000000000002bea0
0x000000007720308e: ntdll!LdrInitializeThunk+0x000000000000000e
0x00000000773d0fca: ntdll_773b0000!NtOpenKeyEx+0x0000000000000012
0x0000000075b42721: KERNEL32!LocalBaseRegOpenKey+0x000000000000010c
0x0000000075b428c9: KERNEL32!RegOpenKeyExInternalW+0x0000000000000130

--------------------------------------
Parsed 0x358E stack traces.
Dumped 0x7 stack traces.
0:044> !handle 242c ff
Handle 242c
  Type          File
  Attributes    0
  GrantedAccess 0x120089:
         ReadControl,Synch
         Read/List,ReadEA,ReadAttr
  HandleCount   2
  PointerCount  3
  No Object Specific Information available
Julian
  • 20,008
  • 17
  • 77
  • 108
danielm
  • 41
  • 1
  • if you see at the very top of the question, there is a link to Microsoft Connect with a bug report and a sample project that reproduces the error: https://connect.microsoft.com/VisualStudio/feedback/details/558848/vsip-rebuilding-a-project-with-open-designers-twice-causes-an-error-with-locked-bin-debug-dlls – Julian Feb 24 '11 at 09:45
  • Disabling the hosting process worked for me. It continues to work after re-enabling it as well. I spent 4 hours trying to fix this issue trying hundreds of solutions. This is the only one that even remotely seemed to work. – Drew Chapin Oct 05 '15 at 20:44
3

Here's another possibility:

After receiving this error in vs2012 / win7, I went and tried to delete the file in the bin directory and explorer indicated that the file was in use by the XAML UI Designer.

I closed all the tabs I had open in VS, closed VS, then made sure to kill all MSBuild processes in taskmanager. Finally, after restarting VS I was able to build the solution.


and another possible cause:

I have noticed another possible cause for this issue. After doing some code refactoring, moving projects in and out of a solution, my project references were no longer referencing the projects in the solution as expected.

This mislead visual studio to think it could build some projects concurrently, thus creating the file locks.

EDIT: I have had this happen on a few occasions even recently with VS2012 and it does fix it once I set the build order to the correct dependencies, kill any msbuild processes that VS left running, and then restart VS. I kill the msbuild processes just to be sure, but closing VS should kill them off too.

What I usually do to cause this is refactor a project such that it relies on another project within the solution that it wasn't referencing on last build. This sometimes seem to confuse VS and it doesn't update the build order.

To check build order: Right-click the Solution in the Solution Explorer and select "Project Build Order..." and verify that dependencies are properly noted for each project.

Kevin Shanahan
  • 109
  • 1
  • 9
  • We recently experienced this on a WinPhone 8 project. Inexplicably, the cause was using the Tuple type. Removing the code that used a Tuple the problem went away. Add the code back the problem returned. – Seamus Jun 05 '13 at 12:43
  • I had same issue with VS2012, closing VS did not do the trick - have to kill all the msbuild.exe tasks manually – mizzle Jul 10 '14 at 16:24
  • I'm using VS 2013 and I was able to just kill the "XDesProc.exe *32" process (Microsoft Visual Studio XAML UI Designer) in task manager prior to each build and that did the trick. No need to restart VS because the XAML UI designer seems to reload every time you open a *.xaml file in design view. – Tim Sexton Mar 23 '16 at 20:41
3

Restart IIS- could be a process attached to the debugger

Alan
  • 281
  • 2
  • 9
3

Disable antivirus and try. I was also facing that problem... but in my case antivirus was blocking my application when I disabled antivirus it resolved.

3

I faced the same error.

I solved the problem by deleting all the contents of bin folders of all the dependent projects/libraries.

This error mainly happens due to version changes.

Utsav Dawn
  • 7,896
  • 2
  • 29
  • 44
2

This has been filed multiple times on Connect, Microsoft's community bug reporting site. FYI, I believe this bug has afflicted Visual Studio since 2003 and has been fixed after RTM each time. :( One of the references is as follows:

https://connect.microsoft.com/VisualStudio/feedback/details/568672/handles-to-project-dlls-are-not-released-when-compiling?wa=wsignin1.0

Michael
  • 3,821
  • 2
  • 19
  • 18
1

Do the simple things first.

Check that part of your solution is not locked by a running process.

For instance, I ran "InstallUtil ' on my windows service(which I normally unit test from a console).

This locked some of my dlls in the bin folder of the windows service project. When I did a rebuild I got the exception in this issue.

I stopped the windows service, rebuilt and it succeeded.

Check Windows Task Manager for your Application, before doing any of the advance steps in this issue.

So when you hear footsteps, think horses not zebras! (from medical student friend)

Julian
  • 20,008
  • 17
  • 77
  • 108
GregJF
  • 456
  • 4
  • 14
1

I had same problem. It said could not copy from bin\debug to obj.....

When i build web project i found my dll were all in bin folder and not in bin\debug. During publish vs was looking for files in bin\debug. So i opened web project file in editor and look for instances of bin\debug and i found all the dll were mentioned as bin\debug\mylibrary.dll. I removed all \debug from the path and published again. This time vs was able to find all the dll in bin folder and publish succeeded.

I have no idea how this path got changed in web project file.

I spent more than 5 hours debugging this and finally found solution on my own.

This is the right answer.

NCCSBIM071
  • 1,207
  • 1
  • 16
  • 30
1

If none of the above works, and you are developing a console application:

Try typing any character into Program.cs, then delete it. I have no idea why this works, but it seems to resolve 'Unable to copy' problem every time.

Greg
  • 126
  • 6
1

This is rather commonly caused by Avast.

I can usually run my projects in Release regardless, but when running in debug it would pretty regularly fail.

I just add an exclusion for my projects folder and the problem seems to go away. I assume this might also be cause by other antivirus software.

1

Renaming the .exe and .pub file worked for me, but really tedious. I also face the problem that I could not do editing during a debug session. Finally I went to the Advanced Security Setting Page, as per:

https://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28%22VS.ERR.DEBUG_IN_ZONE_NO_HOSTPROC%3a11310%22%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22%29&rd=true

I un-select then re-select the "Enable ClickOnce security settings" checkbox. It's been problem free for some days now....

Yee
  • 861
  • 1
  • 8
  • 13
1

For me this was being caused by having a command prompt open in the targeted folder (C:\users\username\source\repos\project\project\bin\debug\app.publish).

Not sure why DEBUGGING requires access to the publish folder, but closing the command window solved the issue for me.

Bassie
  • 9,529
  • 8
  • 68
  • 159
1

In case somebody is running into this while trying to debug a Unit Test or Run a unit test I had to kill the following two processes in order for it to release the file:

processes to kill.

Haggis777
  • 46
  • 4
0

None of the other answers worked for me but closing all open tabs in Visual Studio appears to have solved the problem.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
0

I know this is a very old question, but I recently experienced the "cannot copy from obj to bin" error in VS 2012. Every single time I tried to rebuild a certain project, I got the message. The only solution was to do a clean before every rebuild.

After much investigating, it turns out I had an incomplete pragma warning statement in one of my files that did not prevent the compilation from succeeding, but was somehow confusing VS into keeping the file(s) locked.

In my case, I had the following at the top of the file:

#pragma warning(

That's it. I guess I was attempting to do something a while back and got distracted and never finished the process, but the VS warnings about that particular line were lost in the shuffle. Eventually I noticed the warning, removed the line, and rebuild works every time since then.

Chris
  • 27,596
  • 25
  • 124
  • 225
0

When I faced a similar issue, the only thing that seemed to work was:

  • Right click the project, going to Settings, and making sure that both Debug and Release builds target the same settings, or have the settings in there that the application tries to load or save.
  • Deleting the C:\Users(YourUserAccount)\AppData\Local(YourAppName) folder.
  • Making sure that no files that I had in there were considered "Blocked". Right-clicking my project's included files, I realized that one icon was actually blocked and considered bad because it was downloaded from the internet. I had to click the Unblock button (in example, check this out: http://devierkoeden.com/Images/Articles/Dynamicweb/CustomModules/Part1/BlockedFiles.png - "This file came from another computer and might be blocked to help protect this computer.").
Alexandru
  • 12,264
  • 17
  • 113
  • 208
0

For Windows Services using WCF, I ended the WFC host process and it worked. I hate it when this happens, and it happens randomly at times.

0

My solution has nothing to do with versions, processes being locked, restarting, or deleting files.

The problem was actually due to the build failing, and not giving the correct error. The actual problem was a design flaw:

// Either this should be declared outside the function, or..
SomeObject a = new SomeObject(); 

Task.Factory.StartNew(() =>
{
   while (true)
   {
      a.waitForSomething();
   }
});

// ...this should not be called
a.doSomething(); 

After changing the scope of "a" to outside the function, or not using "a" after Task.Factory.StartNew();, I was able to build again.

This happened when using VS2012 Update 4 on Windows7x64 sp1.

Error message:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3390,5): error MSB3030: Could not copy the file "obj\x86\Debug\xxx.exe" because it was not found.

T.Coutlakis
  • 2,436
  • 1
  • 19
  • 19
0

I have found with VS2013 I get this error regularly. Something that seems to work reasonably well is to perform a Rebuild Solution prior trying to run the application. I found that performing a CLEAN sometimes works, but the Rebuild Solution seems to work more consistently.

mattpm
  • 1,310
  • 2
  • 19
  • 26
0

This helps for me after i remove read only flag from bin directory. http://www.thewindowsclub.com/error-0x80080015-windows-defender-activation-requires-display-name

Suhan
  • 1,434
  • 2
  • 13
  • 28
0
  1. Set another project as startup
  2. Build the project (the non problematic project will display)
  3. Go to the problematic bin\debug folder
  4. Rename myservice.vshost.exe to myservice.exe
wittich
  • 2,079
  • 2
  • 27
  • 50
Peter PitLock
  • 1,823
  • 7
  • 34
  • 71
0

One thing that really helped me was adding the ".exe" on the Debug folder to the Exclusions on my Anti-Virus.

Ricardo F.
  • 496
  • 1
  • 4
  • 11
0

[Solved] Unable to copy exe-file from obj\debug to bin\debug:

I am approaching that you get this error while you was trying to run two windows form one after another such as first loading a form then after sometimes it will automatically disappear and the second form loaded onto the screen.

Basically, you need to close your first form which is running in the background and the main reason behind this error.

To close the first form you have to add these two lines of code in the second form load event handler.

        Form1 form = new Form1();
        form.Close();

This will solve the error perfectly.

Sabir Al Fateh
  • 1,743
  • 3
  • 20
  • 27
0

Re-enabling the Application Experience service of Windows has fixed that kind of problems for me.

See the following links:

I had the problem using Visual Studio 2008, 2010 and 2013 with Windows 7 64-bit.

Wollmich
  • 1,616
  • 1
  • 18
  • 46
  • Finally! Yes. This did the trick for me, too! Wonder why it was disabled in the first place. Thanks for the answer. I tried to fix this weeks ago but just took another look. Been using another dev machine in the meanwhile. – ldobson Dec 30 '21 at 22:51
0

I had the same problem multiple times but two different approaches helped in conjunction. In all cases one fact is interesting, namely that you can still rename the file. If a file is open by a process it's not possible to rename it. So it must be something else than a simple open file handle.

  1. While compiling a solution that produces IL code Defender totally blocked it even if it contained just a few commands. Even adding the directory to the exclusion list of Windows Defender didn't help. I think the virus scan of Windows Defender has a flaw and goes into an endless loop (or a breakdown?) if a file has a certain unfinished state and it blocks the file. But it never recovers except after a PC restart.
  2. I had the no access bug after I had to kill my solution while Debugging because the Debugger stop button didn't work, or maybe also while doing a regular stop using the stop button, I can't remember anymore. Some dlls seemed to remain locked, no delete possible but I could rename them (??). Then I used 'tasklist.exe' to list all the processes and to my astonishment there was a process still listed with my app's name. Without window. And Task Manager didn't list it in 'Details'. Strange. I could use taskkill /PID:... /F (the /F was necessary) and the process went. After that the compilation succeeded and everything was back to normal. Looks like this exe was holding the DLLs, but in a strange way.

It also occurred much more when Oracle VirtualBox was also running. Don't know whether that has an influence. Could also have a connection to the 'Out of Memory' exceptions that sometimes are reported while VirtualBox is consuming a lot of memory.

Now I use both approaches in conjunction whenever the problem occurs. And it still happens from time to time.

0

I tried several solutions that you provided, but occasionally I still receive this error. I am positive that my process is not running, and when i try to delete the executable file with internet explorer it is removed from the file list, but then I press F5 and voila, the file is back. It has not been deleted at all.

But if i delete the file through the TotalCommander, the exe file is actually deleted and I can successfully build the project.

I am using windows 7 x64 and total commander 7.56a 32 bit.

Gico
  • 1,276
  • 2
  • 15
  • 30