3

I encountered this problem

Unable to Copy File "*\bin\debug\*.dll" to "bin\*.dll"

And when this happens, the compilation will fail because of the above error.

Edit: I know that the file is locked by the devenv.exe process that results in the above error because if I restart the VS, then the problem would go away, but is there anyway to fix this without actually restarting the VS?

Kevin Shanahan
  • 109
  • 1
  • 9
Graviton
  • 81,782
  • 146
  • 424
  • 602
  • so if you use proces explorer or the likes, does it actually lists devenv having a handle open to the output file? – stijn Jan 10 '10 at 13:37
  • @stijn, I haven't use process explorer, but I'm very sure that it's VS that does the locking because I can compile if I restart the VS. – Graviton Jan 10 '10 at 14:54
  • The answer below goes into detailed steps showing how to do this without closing VS... great answer, just not sure why you'd want to do that much work. Why not just close/restart VS? – Sam Post Jan 11 '10 at 07:49
  • @Sam: That's because in the long run, deleting the locked resources is far more efficient than restarting VS. – Graviton Jan 11 '10 at 08:45
  • the thing is I occasionally have the problem too, and simply deleting the file solves it; if VS would be locking the file, I wouldn't be able to delete it in the first place.. only happens to me on Windows 7 though, and it seems to occur more frequently if I have an eplorer window open on the output folder. – stijn Jan 11 '10 at 12:31
  • An easy work around is to simply move the file. VS will build again and then you can delete the moved file when you close VS. – Telavian Jan 12 '12 at 18:37
  • answer to this question helped me http://stackoverflow.com/questions/13653835/vs2012-xdesproc-hangs-when-xaml-file-is-opened – Kirill Chilingarashvili Feb 10 '13 at 07:01

4 Answers4

6

I've run into this in the past and as Sam has suggested, restarting VS seems to work, though not what you're looking for.

However, there is a thread on the MSDN forums, "Unable to copy from obj\debug to bin\debug", which provides a couple of suggestions:

Option 1

Create a pre-build action in your project by going to project properties (right-click on the project in the solution explorer, and select the Properties option), select the Build Events tab. Add this code:

if exist "$(TargetPath).locked" del "$(TargetPath).locked" if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"

This copies the file to a different name, and allows the build to continue successfully.

With a subsequent follow up of:

But one small improvement is neccessary to let the hack work also if the build is run from scratch, e.g. after clean:

if exist "$(TargetPath).locked" del "$(TargetPath).locked" if not exist "$(TargetPath).locked" if exist "$(TargetPath)" move "$(TargetPath)" "$(TargetPath).locked"

Option 2

I had a similar problem and I solved it excluding from the project in [Source Repository of Choice] the folders bin and obj.

HTH's

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
2

FWIW, this bug has been around for YEARS and MS still haven't fixed it. Just google "MSB3021" to see all the posts about it.

Rob King
  • 94
  • 3
1

I've seen this problem too before, if I remember right I just closed Visual Studio and started it back up again. If you know the process which is locking your file, you need to find a way to a) get the process to release the lock, b) kill the process

Sam Post
  • 3,721
  • 3
  • 17
  • 14
0

I seen this problem , I went to the debug folder of my project in VBprojects folder and deleted the files , then I rebuild the application and it works