Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2010 : Unable to copy file on debugging

I'm having problem from a while (since I'm using visual studio 2010 I think). When I'm trying to start a debugging on my Web Application, I randomly get this error from VS.net environment :

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

The Only way to get rid of this error, is to restart Visual Studio (to get the error back soon something like twice a day).

After searching a bit on the internet, I found out thats it's possible to add a pre-buil, from here MSDN Link

All I found is to write down something in the pre-build events and do some file handling ... well (it do not work anyway).

Question 1 : Is there any easier way to solve this problem (and for allways!)
Question 2 : What is the exact reason of this problem?

like image 357
Simon Dugré Avatar asked Apr 15 '11 13:04

Simon Dugré


1 Answers

This is related to shadow copying. You can disable it like this in web.config:

hostingEnvironment shadowCopyBinAssemblies="false"

You can also use a workaround if the above settings doesn't work at all with a pre-build event:

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

You can completely disable the ShadowCopy service in Windows so that you won't have to set these values for all solutions, but that will break a lot of functionality so I don't recommend it.

You can always use a custom batch script to get called by the postbuild event of the last project (as per build order) which will do all the copying business (which I use now).

like image 85
Teoman Soygul Avatar answered Oct 08 '22 04:10

Teoman Soygul