Ask a questionAsk a question
 

AnswerUnable to copy from obj\debug to bin\debug

  • Saturday, January 14, 2006 1:06 AMrfwilliams76 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I get the following error when I try to run my program.

     Error 1 Unable to copy file "obj\Debug\HomeInventory.exe" to "bin\Debug\HomeInventory.exe". The process cannot access the file 'bin\Debug\HomeInventory.exe' because it is being used by another process. HomeInventory

Answers

  • Tuesday, April 11, 2006 9:34 AMMatthew Wright Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    OK, I've found out how to sort it out. I ran a program which figured out which application had the file locked, and it turned out to be devenv.exe, i.e. Visual Studio itself. A bit more searching and I found a fix.

    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.

    Hope that helps anyone else who's got this problem.

    Matthew

All Replies

  • Monday, April 10, 2006 11:03 AMMatthew Wright Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I get this same error when I'm trying to build a class library which contains custom controls. It seems that visual studio has it locked: I can restart visual studio, or switch to a different project and back again, and I can build the library successfully, once. I can then run the app in debug, but if I make changes and try to run it again I'm back to getting the 'Unable to copy file...' error. It must be something to do with the particular library, as I have other libraries with custom controls in them which work fine.

    Any help would be greatly appreciated as this is really slowing down my development.

    Matthew

  • Tuesday, April 11, 2006 9:34 AMMatthew Wright Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    OK, I've found out how to sort it out. I ran a program which figured out which application had the file locked, and it turned out to be devenv.exe, i.e. Visual Studio itself. A bit more searching and I found a fix.

    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.

    Hope that helps anyone else who's got this problem.

    Matthew

  • Tuesday, March 13, 2007 10:13 AMricardofiel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

    I had the same problem after converting a VS2005 WebSite to Web Application Project. It has been a pain everytime i need to compile.

    The above code solved the problem. Thanks!

    Ricardo

  • Monday, May 14, 2007 7:10 AMPetr Bouda Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    This has solved my greatest pain in last few days. Thanks.

     

    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"

     

     

    It works for me. I hope it would work for anybody else who will need it.

     

    Petr

  • Tuesday, May 15, 2007 10:09 AMdavierik Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The supplied pre-build code works for me, once. But after the one successful build, VS is back to throwing errors at me again.

    Is there some reason as to why the pre-build commands won't run properly every time?
  • Monday, May 28, 2007 6:56 AMPetr Bouda Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The first reason I can imagine is some lock on the file. I've believed that this should be definitely solved by my patch, but maybe I am wrong in some way. I would suggest you to study content of the "Output" window in VS and try to figure out what goes wrong. If you find the error, you can post it.
  • Tuesday, February 05, 2008 3:53 PMSeshu Palachulla Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Look for the  Project.exe and Project.exe.vshost etc in the TaskManager and clear them off.

    Then try to build/ Run the projectSee if that helps.

     

    Thanks

    Seshu

     

  • Thursday, February 07, 2008 1:09 AMJaideepV Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    Thanks Petr.

     

    works for me as well !!

     

     

  • Tuesday, March 04, 2008 6:51 PMPeaches June Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer

    Did anyone actually check to see if the file in question was actually locked.  I had a very similar problem and found that my .dll and .pdb files had somehow been checked into Visual Source Safe (VSS), which in fact had locked both files.  I did have to manually delete them from the bin directory which also asked if I wanted to delete them from VSS, and I of course replied "YES".

     

     

    • Proposed As Answer byAllastanao Wednesday, December 17, 2008 7:56 AM
    •  
  • Tuesday, November 25, 2008 8:38 PMNate Tregillus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer

     

    This solution looks well and good, but for visual studio to be locking your own dll files. It sounds like a bug in project references.

     

    anyone submit this one?

    • Proposed As Answer byAllastanao Wednesday, December 17, 2008 7:56 AM
    •  
  • Wednesday, December 17, 2008 7:58 AMAllastanao Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I had a similar problem and I solved it excluding from the project in VSS (Visual Source Safe) the folders bin and obj. Then all worked.
  • Thursday, January 15, 2009 11:18 AMAlexandre Neukirchen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     
    Bruno said as I did and it worked!
    But someone knows why this happens?
  • Thursday, January 14, 2010 11:08 AMsanthosh_iyer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    An easier way is to manually delete the "obj" folder and rebuild the project. This usually works for me.