32

For one of my ASP.NET 3.5 applications, every single time I try to build the web app, it throws the following build errors in Visual Studio 2008:

Error 165 Unable to copy file "C:\InOne\Common\DexProcessor\bin\Debug\DexProcessor.dll" to "bin\DexProcessor.dll". The process cannot access the file 'bin\DexProcessor.dll' because it is being used by another process. InVision2 Error 166 Unable to copy file "C:\InOne\Common\DexParser\bin\Debug\InOne.DexParser.dll" to "bin\InOne.DexParser.dll". The process cannot access the file 'bin\InOne.DexParser.dll' because it is being used by another process. InVision2 Error 167 Unable to copy file "C:\InOne\Common\AlertProcessor\bin\Debug\InOne.Invision.AlertProcessing.dll" to "bin\InOne.Invision.AlertProcessing.dll". The process cannot access the file 'bin\InOne.Invision.AlertProcessing.dll' because it is being used by another process. InVision2 Error 168 Unable to copy file "C:\InOne\Common\InVision.BusinessLogic\bin\Debug\InVision.BusinessLogic.dll" to "bin\InVision.BusinessLogic.dll". The process cannot access the file 'bin\InVision.BusinessLogic.dll' because it is being used by another process. InVision2 Error 169 Unable to copy file "C:\InOne\Common\InVision.Common\bin\Debug\InVision.Common.dll" to "bin\InVision.Common.dll". The process cannot access the file 'bin\InVision.Common.dll' because it is being used by another process. InVision2 Error 170 Unable to copy file "C:\InOne\Data\bin\Debug\InVision.Data.dll" to "bin\InVision.Data.dll". The process cannot access the file 'bin\InVision.Data.dll' because it is being used by another process. InVision2 Error 171 Unable to copy file "C:\InOne\Common\InVision.DataAccessLayer\bin\Debug\InVision.DataAccessLayer.dll" to "bin\InVision.DataAccessLayer.dll". The process cannot access the file 'bin\InVision.DataAccessLayer.dll' because it is being used by another process. InVision2 Error 172 Unable to copy file "C:\InOne\Common\InVision.DataAccessLayer.SqlClient\bin\Debug\InVision.DataAccessLayer.SqlClient.dll" to "bin\InVision.DataAccessLayer.SqlClient.dll". The process cannot access the file 'bin\InVision.DataAccessLayer.SqlClient.dll' because it is being used by another process. InVision2

This just started happening a week ago and is very annoying... I have to go into the web app's bin folder and delete the pdb files and then it'll let me delete the dll's most of the time. Every once in a while it doesn't let me so I have to close Visual Studio and then it lets me delete them. I checked and it's Visual Studio (devenv) that is locking the dll's. Rebooting the machine doesn't help.

This is really reducing my productivity, is there anything I can do to resolve this?


As mentioned, Visual Studio 2008 (devenv.exe) is the process locking the DLLs.

I noticed something... When it compiles successfully, it's copying all the DLLs into the bin folder, then they are all deleted, then a new set are copied into the bin. When it isn't successfull, the first set of DLLs are copied in, then it fails. So it seems to be using the bin folder for 2 things when it should only be for 1. Does this help??

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Justin
  • 17,670
  • 38
  • 132
  • 201

32 Answers32

19

The issue ended up being that in the web.config someone had added:

hostingEnvironment shadowCopyBinAssemblies="false"

After commenting this out, everything started building ok. What a nightmare!!

AMissico
  • 21,470
  • 7
  • 78
  • 106
Justin
  • 17,670
  • 38
  • 132
  • 201
13

Use ProcessExplorer to find out what process has the file open and go from there.

If a process is currently using those DLL, you can't delete and re-write it. You'll have to kill or otherwise stop the process using those DLLs while you compile.

Ben S
  • 68,394
  • 30
  • 171
  • 212
12

I have battled this issue FOR YEARS!

Have you tried adding this to your PREBUILD Event?

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

See this for more info: http://nayyeri.net/file-lock-issue-in-visual-studio-when-building-a-project

Here's another thread, with more things to try...

http://social.msdn.microsoft.com/forums/en-US/Vsexpressinstall/thread/5b71eb06-5047-483d-8fd3-b75c102d41e9/?prof=required

Leo
  • 37,640
  • 8
  • 75
  • 100
10

What worked for me is the following 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" "$(TargetFileName).locked.bak"
if exist "$(TargetPath)" ren "$(TargetPath)" "$(TargetFileName).bak"

What I have noticed in my case is that the 2 files are being created and can not be deleted. You can, however, rename them (and they are still in use if you try to delete them). On a next build, the renamed files are no longer in use (lock removed) and they can be deleted, which is what the above script does, after which it can safely rename the new locked files so there will be no problems in generating the build output.

The other pre-build events posted here and in other places did not help me a lot (they worked only for one extra build or only a few before the problem arose again). So now I am currently using the one posted above for my debugging purposes.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
Marcel Gheorghita
  • 1,853
  • 13
  • 19
  • 5
    I think the target of the `ren` commands needs to use `$(TargetFileName)` since the second parameter to `ren` is not supposed to include the path. – Jeremy Stein Sep 28 '11 at 16:29
4

I just wanted to say that this problem started with me today. (VS 2010, C#) I have been working on this program for a month without this problem, now today it started. I start VS, change code, compile and test and quit program. Make another change, compile and BOOM Unable to copy file "obj\x86\Debug\progname.exe" to "bin\Debug\progname.exe" because if is being used by another process.

ProcExp shows only Visual Studio (actually devenv.exe) using this file. There is only one instance of VS running. There are two listing on my debug\progname.exe, one is a Type DLL, the other is a Type handle.

Using devenv /ResetSettings did not resolve anything, but wasted 10 minutes putting everything back to my desired view.

Using the PREBUILD events rename trick mentioned above solves the problem for a couple of changes, but on the next change the "exe.locked" file is locked and cannot be deleted. Then the rename fails.

The debug\progname.exe file name remains locked even after closing the project.

Closing VS, manually delete the files in the debug folder, opening VS and my solution, then Build->Clean Solution seems to work for me, at least its working now after I did all of that stuff.

Hope this helps -rwg

rwg
  • 41
  • 1
4

I didn't have any luck with the pre-build events unfortunately. What worked, in typical IT Crowd fashion, was to quit Visual Studio and reopen it.

2

If you have Visual Nunit, it must be locking dll file.

  1. Close VS
  2. Go to taks manager , kill the Visual Nunit process
  3. Now open VS and build the project
Taryn
  • 242,637
  • 56
  • 362
  • 405
2

Easy fix for Windows 7: Start the service "Application Experience" . Search for "Services" in "Control Panel".

-Martin

1

Hi I'm facing the same issue for a little while. It's very annoying.

I have an easier yet not so efficient solution for the problem. Cleaning the project or solution solves the problem.

1

Just go to \Debug\bin and delete all .dll files.

Working great for me.

learn_andrd
  • 1,046
  • 1
  • 12
  • 15
1

I had this issue on a web project with System.Web.Extensions.dll from the Microsoft Reference Assemblies folder. Setting "Copy Local" to false in the reference properties fixed it.

tags2k
  • 82,117
  • 31
  • 79
  • 106
0

I had similar issue and was able to resolve it by changing the 'AssemblyInfo.cs'

Visual Studio build fails: unable to copy exe-file from obj\debug to bin\debug

Community
  • 1
  • 1
lulalala
  • 17,572
  • 15
  • 110
  • 169
0

You could download the excellent SysInternals Handle program. This will tell you which processes have a lock on the files concerned.

If it is an external program (e.g. virus scanner/indexer) then this should help. If it just reports Visual Studio (devenv.exe) as the culprit, then it will be of less help!

Rob Levine
  • 40,328
  • 13
  • 85
  • 111
0

I'm assuming you already know it's VS2008 which is locking the files. You can try running MSBuild from the command line and see if the problems disappear. Unfortunately Visual Studio can keep files locked when it shouldn't, in some hard-to-predict scenarios.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
0

File locks are just part of working with Visual Studio. There aren't any great ways to get around this problem.

Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
  • 2
    Hi guys. To the first two, I already checked and it's Visual Studio that is locking the files. To Chris, how can that be an answer?? Pretty much everytime I build the web project it fails because the referenced project dll's that are being copied to the web bin are in use by Visual Studio. This means that I need to close and reopen Visual Studio in order to build almost every time... this is not a solution! This is not happening in under solutions or project so it's something specific to this one web project, so saying this is just part of Visual Studio is crap... – Justin Jul 13 '09 at 19:42
  • Flip the switch on the "copy assemblies locally" option – Chris Ballance Jul 13 '09 at 20:28
  • 1
    Thanks that gets me close. The only build error left is: "Unable to copy obj\debug\web.dll to bin\web.dll. The process cannot access the file because bin\web.dll because it is being used by another process." – Justin Jul 13 '09 at 21:49
0

There was a specific bug in Visual Studio 2008 which was fixed in SP1 which may be your issue. It occurs when you reference an embedded JavaScript file and causes the problem you are seeing. See here for more details.

Jon
  • 5,247
  • 6
  • 30
  • 38
0

Happened to me just now. Had to kill all devenv.exe processes (there were 3 of them after closing VS 2010 window).

Mauricio Ramalho
  • 849
  • 1
  • 8
  • 15
0

Delete the binaries from the bin\Debug folder and recompile them. This works for me!!!

Subbu
  • 1
0

This issue commonly occurs when you change your project from one directory to another . For the Shadow copy error you might have added this line in your web.config.To fix this follow the following

In your web.config file if there is something like

<hostingEnvironment shadowCopyBinAssemblies="false" />

change that into

<hostingEnvironment shadowCopyBinAssemblies="true" />

or remove it .Then it will work fine

Prashanth
  • 3
  • 2
0

I was using Visual Studio 2012 when this started happening on a 7 year old solution (for the second or third time: I've been to this question before).

I tried the various voodoo. I cleaned the solution. Didn't work. I restarted Visual Studio. Didn't work. I was confident that last one would work, because that is the voodoo that worked last time.

Ultimately, I remembered a security update had installed last night and was configured when I started my machine this morning - (Connected or not? No Idea) - So, I restarted Windows, and voila, it all worked like magic again.

Thanks MS for more mind mincing.

argyle
  • 1,319
  • 2
  • 14
  • 28
0

I also face this problem. First i try to delete contentious .dll but it shows Access denied, then i close my VS and after open, it works fine.

Mehedi hasan
  • 91
  • 1
  • 2
0

Similar to Benoit's answer but doesn't require any tool installation, you can use tasklist (task manager) command at the command line with the '/m' switch to get a list of processes using the dll:

tasklist /m mylocked.dll

I saw some posts indicating you have to do it from the directory of the offending dll, but I haven't found that to be the case.

bitcoder
  • 1,217
  • 12
  • 22
0

I did had simillar problem solution for me was to look at the *.csproj file and under i found missing file so and below was correct one so i just removed lines and it worked straight away

Marcin
  • 1
0

you can also delete bin and obj folders in all projects in solution then rebuild solution.

HamidReza
  • 1,726
  • 20
  • 15
0

Removing the following lines from my app.config solved this for me - I'm using VS2010.

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
          <assemblyIdentity name="nunit.framework" publicKeyToken="96D09A1EB7F44A77" culture="neutral"/>
          <bindingRedirect oldVersion="0.0.0.0-2.5.7.10213" newVersion="2.5.7.10213"/>
      </dependentAssembly>
  </assemblyBinding>
</runtime>
Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
0

Check if the user dll and the application referencing the dll target the same .NET framework. I had a case where the frameworks were different which caused this problem.

0

open your project in explorer click the property of the bin folder and uncheck the read only property this one works in my xamarin forms project

0

I finally how fix it. It Occures because the first debug exe still running. So , go to Task Manager -> Process Tab -> [your project name exe] end the exe process

Sanket Parchande
  • 894
  • 9
  • 14
0

The file cannot be deleted, fortunately can be renamed and moved. So I created prebuild batch (using date and time as random string, there can be easier ways):

For /f "Tokens=2,3,4 Delims=/. " %%i In ("%Date%") Do @(
  Set Month=%%i& Set Day=%%j& Set Year=%%k
)

set ActDate=%Year%-%Month%-%Day%

For /f "Tokens=1,2,3 Delims=/.:, " %%i In ("%Time%") Do @(
  Set Hour=0%%i& Set Min=%%j& Set Sec=%%k
)

set ActTime=%Hour:~-2,2%-%Min%-%Sec%

move c:\MyProject\bin\Debug\myproject.exe c:\garbage\%ActDate%_%ActTime%_myproject.exe
John Conde
  • 217,595
  • 99
  • 455
  • 496
T_xy
  • 1
0

Encountered this problem. Got to know that I was already running the project through git bash. Stopped it. And now its working fine.

Hence there's a possibility if you have been running the same project from anywhere in your local and this error occur. So stop that running program and do it again.

Rehmanali Momin
  • 199
  • 1
  • 1
  • 14
0

I have resolved error by Terminate .exe under TaskManager --> details.

Now build your solution

enter image description here

Shakeer Hussain
  • 2,230
  • 7
  • 29
  • 52
0

This happens to me sometimes when using Visual Nunit for unit tests.

It seems the process 'VisualNunitRunner.exe' locks the .dll files in the destination directory.

I used Unlocker to find the process, kill it or unlock the files.

warrickh
  • 1,616
  • 1
  • 16
  • 14