Unable to copy from obj\debug to bin\debug
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
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
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
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
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
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
- 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? - 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.
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
Thanks Petr.
works for me as well !!
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
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
- 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.
- Bruno said as I did and it worked!
But someone knows why this happens? - An easier way is to manually delete the "obj" folder and rebuild the project. This usually works for me.
