Gary Farr

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
Leave a Comment

(required) 

(required) 

(optional)

(required)