Keyvan Nayyeri

My daily musings about software and technology

File Lock Issue in Visual Studio When Building a Project

One of the common issues for Visual Studio users is a file lock error when trying to build projects.

This is a generic error for all developers but is more common among Visual Studio Extensibility developers who try to build add-ins and debug them. I see that a newbie has to wrestle with this issue when developing an add-in.

Here I just want to show one of the several ways to work around this issue and of course, want to talk about a simple and easy to use solution. This has been a question for a few readers of my book and would be worthwhile to be mention here.

First let me describe the issue in more details. Sometimes when you try to build a project in Visual Studio, you get a build error that specifies that Visual Studio is unable to copy your new assembly file. The error text is something like this which usually comes with a warning with same details:

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

And here is the snapshot of the error:

Error

The reason to see this issue is your assembly is locked by one of the previous processes so Visual Studio is unable to delete the previous assembly and copy the new one so your build process fails.

There are several ways to reproduce this issue but I'm sure that you'll experience it after writing 2-3 add-in projects.

But how to solve this? There are various ways based on your project type but one simple solution that I recommend to Visual Studio add-in developers is to add a simple code to their project's build events.

You can add following lines of code to the pre-build event command line of your project.

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

To add this code, you need to get access to your project properties and then open the Build Events and add the code as is shown below.

Modify Build Events

This simply solves the issue. But how does this code work? It simply copies the locked file to a new location and lets your build process to be followed.

50 Comments

Mike Heath
Apr 22, 2008 8:30 PM
#

Why is this STILL a problem on Windows? Linux, OS X, and other Unix like operating systems don't have this limitation. Why are we stuck with a second class file system in Windows?


Tim
Aug 28, 2008 9:04 PM
#

Thanks, but I've had your code in my build event for quite a while and it doesn't work.


Nasser
Sep 22, 2008 9:31 AM
#

Thank you for your solution!

I am not building an Add-on, but had been getting this error for a while. I thought for a while that this might be caused by a cyclical reference, but found none. Your code nippet solved the problem.

Best regards


zobidabee
Nov 17, 2008 2:47 PM
#

you cannot move or rename a file if it's already used by another process... If you create an Addin, you need to unload it from your IDE, then quit all visual studio instances, then reload your addin project.

the reason is quite obvious, once you built your addin and added it to one instance of VS, it has been loaded, so you need to unload it if you want to rebuild it. but you may encounter a strange (for me) behaviour:if you didn't close your AddinSolution, you open another IDE, create a solution which will instanciate your addin classes, during the first load you can rebuild as many times as you want... but once your addin solution is closed, you open it for fixing some lines then you encounter the problem.


Ty
Apr 01, 2009 4:42 PM
#

It works 90% of the time, thank you!


Haggis
Apr 23, 2009 9:10 AM
#

Works well with XBAP's as this issue was aging me.


aj
Jun 25, 2009 5:09 PM
#

Hey this solved my long-standing issue! One of my class library projects would do this whenever I made a change and re-build the UI project referencing it.

I no longer have to exit VS and re-open

Thanks!!!


Justin Toth
Jul 14, 2009 11:56 AM
#

Sadly this doesn't work for me... :(


Thankyou
Aug 25, 2009 1:50 PM
#

Works , need to give this option for every depended projects prebuild events.


dipesh
Sep 23, 2009 8:11 AM
#
sometime the folder directory is readonly thats why it wont build ur solution change the rights

BEM
Apr 14, 2010 12:39 AM
#
Worked on VS 2010 RTM. Thanks!!

H.Dolder
Apr 20, 2010 3:53 PM
#

Worked on VS 2010 Professional RTM. Thanks!!


Matt
May 19, 2010 5:44 PM
#

Hey thank you for this article. I have been banging my head againist the wall for a couple of days now.


James Coverdale
Jun 07, 2010 7:38 AM
#
Thanks, worked perfectly, before this fix i was having to close VS down everytime i wanted to build!

Jody
Jun 09, 2010 4:48 PM
#
Thanks, I just upgraded my project to VS.Net 2010 and started to get this problem. Your solution worked great. Thanks So much.

dave davies
Jun 25, 2010 2:16 AM
#
Thanks from me too.
Upgraded to VS 2010 and started to get this problem.

Tony
Jun 29, 2010 8:37 PM
#
Many thanks
Tried many other techniques but none worked

rtf_leg
Aug 28, 2010 7:22 AM
#
Thank you! You solve this damned LNK1168 error forever :-)

Sunil
Sep 16, 2010 5:46 AM
#
This is not working in my case .I have migrated the project from VS 2008 to VS 2010 .Stll i am facing the same issue.
Did i missed some thing

Rick
Nov 11, 2010 8:11 AM
#
Thanks very much ! It works correctly, but is this a VisualStudio bug?

Gopal
Nov 26, 2010 5:05 AM
#
Thanks, worked for me (VS 2010)

Jose
Nov 30, 2010 9:45 AM
#
Nice, thanks!

bostjan
Dec 01, 2010 3:33 PM
#
Ususally the issue gets superturbulent if you use automated assembly version incrementation for controls library or any other dll library so to speak. Just avoid using that "asterisk" in asembly version id. VS assembly version to match up old new library version for internaly managed instance of app for design .... SHHORTLY: Avoid any automatic assembl version incrementation

Gokcer Gokdal
Jan 14, 2011 3:46 PM
#
You are great! Works like a charm. Thanks a lot..

Taija
Jan 15, 2011 8:07 AM
#
Thanks, You saved my day! :D

x
Jan 18, 2011 1:47 AM
#
I have written a script that automatically increments the assembly version number. This was necessary to solve some deployment issues i had.
As soon as I use the script it as goes savage. Very Very disappointing.

Dan
Jan 19, 2011 3:46 PM
#
work for me

Sadegh
Feb 17, 2011 1:36 PM
#
Thanks keyvan

Niidii
Apr 06, 2011 4:12 AM
#
I'm using VS2010 to write an add-in and your first suggestion didn't work. Then I noticed that the file it was complaining about was located in the bin-folder. So I added the following lines to the pre-build event:


if exist "$(ProjectDir)bin\$(TargetFileName).locked" del "$(ProjectDir)bin\$(TargetFileName).locked"
if exist "$(ProjectDir)bin\$(TargetFileName)" if not exist "$(ProjectDir)bin\$(TargetFileName).locked" move "$(ProjectDir)bin\$(TargetFileName)" "$(ProjectDir)bin\$(TargetFileName).locked"

...and it worked.Thank you!

Dave
Apr 29, 2011 3:13 PM
#
thanks man. worked for me

sushma
May 02, 2011 5:11 PM
#
thanks..:) it worked for me

Miguel
Jul 12, 2011 5:05 PM
#
It worked for me too. Thanks!

Gihad
Jul 21, 2011 11:59 AM
#
Does not work for me.

It does rename the file but the another instance of the file is re-created automatically.

Similarly, If I manually delete the file through windows explorer, another instance of the file is created right away.

ragshas
Aug 05, 2011 12:19 AM
#
it only works for the first time and then it doesn't work at all.
i pasted the code.what more should i do

Amir Hossein
Aug 11, 2011 2:35 PM
#

Thanks Keyvan. This helped me.


Zia
Dec 22, 2011 2:55 AM
#
Thanks.

mohammad
Mar 10, 2012 7:00 AM
#
Thank you!
It's really helpful

mk
Apr 24, 2012 5:06 AM
#
worked!

hakeem
Jul 08, 2012 5:32 AM
#
Thank you very mach my brother

Gamwanga Paul Edmond
Jul 13, 2012 3:48 AM
#
Man yo are a genius this code works for me
thank you very much

padam
Aug 01, 2012 6:59 AM
#
Thank you for ur help it worked fine

P.VenkatRaman
Sep 28, 2012 1:01 AM
#
Thanks it worked

sr
Oct 17, 2012 7:44 PM
#
worked. thanks.

Matias
Dec 27, 2012 6:59 AM
#
THANKS SO MUCH!!!!

Suresh
Jan 29, 2013 4:42 AM
#
Its working....
Thank you....

christopher
Mar 27, 2013 1:44 AM
#
Thanks dude.It's working fine..

afzal khan
Apr 10, 2013 4:59 AM
#
thank you so much

jindrich
Apr 10, 2013 11:40 AM
#
It does not work for me - the output file locked by VS simply cannot be deleted (neither by script nor by hand) since it is locked by VS :-(
Have I missed something? Thanks.

Maxx
Apr 26, 2013 6:14 PM
#
This code does has been suggested by people, and it does NOT work. It would be nice to hear from someone who's actually solved the issue.

realtan
May 16, 2013 3:54 AM
#
thanks!!!!!!!!!!!!!!!!

Leave a Comment