error MSB3021: Unable to copy …

vs - transparent bg

Sometimes common approach to develop the particular feature of a software can be wrong for the principles of software quality. I was on coding on one of my free time projects and all the sudden the project that was working a minute ago started to give this error message:

Error 1 Unable to copy file "obj\x86\Debug\PROJECT_NAME.exe" to "bin\Debug\PROJECT_NAME.exe". The process cannot access the file 'bin\Debug\PROJECT_NAME.exe' because it is being used by another process. c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets 2868

It’s actually one of the most famous problem developers face. I don’t know is there any other case for this to happen but usually this happens when we try to build project in Visual Studio, Visual Studio is fails to copy new assembly file.

The reason why visual studio cannot copy our new assembly file is assembly is locked by one of the previous processes – which was suppose to be terminated but somehow still on the run – and Visual Studio is unable to delete the previous assembly and copy the new one so our build process fails. I’m not gonna describe what kind of process happened in run time for the sake of project but I’m gonna be honest and admit it was my fault by poor cooding. ( I can and I could do better:/ )

But how to solve this? There are various ways based on your project type but one simple solution that I recommend is to add a simple code to the 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. It simply copies the locked file to a new location and lets your build process to be followed.

msb3021

 

Yorum bırakın

Filed under C#, Visual Studio 2010

Bir Cevap Yazın

Aşağıya bilgilerinizi girin veya oturum açmak için bir simgeye tıklayın:

WordPress.com Logosu

WordPress.com hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Twitter resmi

Twitter hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Facebook fotoğrafı

Facebook hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Google+ fotoğrafı

Google+ hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Connecting to %s