Farr far away....

gary farr's blog
in

Unable to copy file "obj\Debug\xyz.dll" to "bin\Debug\xyz.dll".

Unable to copy file "obj\Debug\xyz.dll" to "bin\Debug\xyz.dll". The process
cannot access the file 'bin\Debug\xyz.dll' because it is being used by
another process.

I have been getting this error on the project I am working on for quite some time now.  It is an annoying error as the only solution before was to restart the IDE which in my case was a headache as it would take 10 minutes to reload being such a big project.   In doing some research, I discovered that the library that was locking up was doing so because of the way it was being referenced in regards to its project and other project.  It seems if you have a library that is referenced by a library in another project and that 2nd project library is referenced in a 3rd project library, then reference the third project library in the 1st project and then debug through the system, i.e. step through some code, stop the program, re-compile, and this problem will occur.

Alternative attempts to solve this issue:

I first tried to resolve this issue by unlocking to dll from whatever process it was attached to.  I used Unlocker 1.85.  It is a nice tool that displays what processes are currently attached to a library.  It enables you to unlock those processes or kill them.  You can download this tool at http://ccollomb.free.fr/unlocker/.  I have used this tool in the past for similar issues.  However, after I unlocked my particular dll in question and re-compile my code, I still got that error. 

Re-start the IDE.  Obviously this solution is not acceptable, as I stated before.

Final solution:

VS basically locks the file and you cannot use third party resources to unlock it.  Therefore, just use VS!  In the Properties of a project in your IDE you have Buld Events.  Basically, you can write scripts during pre and post builds of a project.  I added these two lines in the pre-build event command line, which basically unlocks the dll within Visual Studio.


IF EXIST $(TargetPath).LOCKED  (del $(TargetPath).LOCKED) ELSE (IF EXIST $(TargetPath) (move $(TargetPath) $(TargetPath).LOCKED))

Re-compiled and your unable to copy dll error will not occur.  I have spent the last day in coding harmony as I have not had to re-start my IDE! <g>

Comments

Zupancic Perspective said:

# May 31, 2007 1:35 AM

richie said:

Great job, thank you very much. It was driving me nuts!

# February 12, 2008 6:12 AM

Doug Wetzel said:

This was a fantastic fix.

Thanks.

# February 22, 2008 8:28 PM

Greg Smith said:

Interesting article!

Thanks..

# February 24, 2008 10:38 PM

Swapnil said:

I am Not Able to find Buld Events in my Sloution Can u help me

# February 29, 2008 9:19 AM

Doug Wetzel said:

to find the "Build Events"

right click the project

Go to the "Compile" tab

Find teh "Build Events" button near the bottom right of the page

# March 19, 2008 7:56 AM

Scott said:

Very handy! Thanks.

# March 22, 2008 2:52 AM

dw said:

I recently discovered that if you set the bin and obj subdirs of your prj to readonly and then reset them to not readonly it works for a while without existing VS...

# May 27, 2008 3:54 PM

Violet said:

what exactly are we supposed to put in the targetPath ? the bin/Debug/<namespace>.dll or wat?

# June 26, 2008 3:47 AM

Violet said:

What exactly are we supposed to enter in TargetPath? bin/Debug/<namespace>.dll or obj/Debug...

please help. My code doesnt compile even once

# June 26, 2008 4:03 AM

Patrick said:

PERFECT!

I was running into this while building an add-in for VS. The add-in would get installed for testing and was supposed to be uninstalled after debugging was over. This works until you exit the IDE, then when you start it up again, the add-in is installed, meaning that the dll is now being used so you could not edit it. Your fix solved it. Perfect!

# July 18, 2008 8:45 AM

Blocked files VS2005 « Billy club Weblog said:

Pingback from  Blocked files VS2005 &laquo; Billy club Weblog

# August 18, 2008 5:09 AM

Sudheer said:

Violet,

You don't need to customize any thing. You just put it as it is.

You can also use the following variation if you want

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

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

# September 22, 2008 10:11 AM

Krishna said:

Still I am getting error Error 141 Unable to copy file "obj\Debug\xyz.pdb" to "bin\Debug\xyz.pdb". Access to the path 'bin\Debug\xyz.pdb' is denied.

How to resolve this ?

by reading ur post i came to know

if exist "$(TargetPath).locked" del "$(TargetPath).locked"  does remove the read only propertly.

if not exist "$(TargetPath).locked" if exist "$(TargetPath)" move "$(TargetPath)" "$(TargetPath).locked"  --- Can you explaint what this statement do?

Thanks in advanced Gary..!

Krishna

# November 12, 2008 10:07 AM

dewraj said:

I am also facing simiialr problem ,

can any one helps us on this?

# December 5, 2008 6:40 AM

Allas said:

Another solution can be check if your obj and bin folders are locked by sourcesafe. In that cases exclude this folders from de project check in VS sourcesafe

# December 17, 2008 1:53 AM

Alexei said:

Nice! i started yestarday writing addin and it get me mads to restart.

thank you very much

# February 4, 2009 5:25 AM

Alwyn said:

VS 2008

I found the 2nd version of the Pre-build event command line above worked but the 1st version above didn't.  i.e. the following worked for me:

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

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

Thanks

# May 14, 2009 5:45 AM

Vishy said:

Thanks a lot great solution..it looks like MS is going back to old versions of VS..great!!

# September 10, 2009 5:53 AM

Sagar said:

Hi,

Please help me with this.

Am using pre build event to delete .obj files associated with the project am building.

The pre build event successfully deletes the .obj files but the problem am facing is that these files are not getting compiled again.

Linker direclty tries to link with the object files that just got deleted and throwing error saying that the .obj file could not be opened..

Anyone having any idea , Please reply.

# September 25, 2009 2:03 PM

Phong said:

Thanks so much! it's very useful for me

# February 8, 2010 1:16 AM

Harshit said:

Gary Farr,

Thanks a lot for your solution. I had been facing this problem for a long time.

Thank you very musch

Regards,

Harshit N Mehta

# February 27, 2010 3:47 PM

Robert said:

I ran into a similar issue but I discovered that even though the bin directory was excluded from the project, TFS still had a lock on the dlls.

So I Included the bin directory in the project > checked out the files > excluded the bin directory > checked in the project and Walla! ... Everything works!

This is probably not the case in all situations but it can be something as little as source control lock or "read only" check.

HTH

Robert

# March 26, 2010 2:02 PM

Marcelo said:

Great work!!!!!!

Thanks.

# May 31, 2010 6:36 AM

Brandon said:

This also solves the issue with the file not found during compile. Three years this solution was posted and the answer is still working. Come on Microsoft, should you not have fixed this on your own?

# August 2, 2010 10:48 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)