Unable to copy file reference.dll to bin/reference.dll. The process cannot access the file reference.dll because it is being used by another process

后端 未结 30 1932
深忆病人
深忆病人 2021-01-31 03:34

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 16

相关标签:
30条回答
  • 2021-01-31 03:49

    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.

    0 讨论(0)
  • 2021-01-31 03:49

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

    0 讨论(0)
  • 2021-01-31 03:51

    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
    
    0 讨论(0)
  • 2021-01-31 03:51

    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

    0 讨论(0)
  • 2021-01-31 03:53

    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.

    0 讨论(0)
  • 2021-01-31 03:54

    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.

    0 讨论(0)
提交回复
热议问题