locked
Unable to copy file during build

    Question

  • When I try to build my solution I often get this error:

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

    The project it's trying to build is a class library referenced by a couple of other projects in the solution.

    The solution builds fine the first one or two times after opening Visual Studio, but after that the only way I can get it to build is to restart Visual Studio.

    Any ideas what's wrong?

    Thanks

    Friday, October 27, 2006 4:40 PM

Answers

  • I'm not sure why this happens, but try adding these two lines to the "Pre-build event command line" in the MyProjectXXX properties (the line break before "move" has been added by the forum):

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

    Monday, October 30, 2006 10:39 AM

All replies

  • When you created a reference to the project, did you do a project reference (via Add Reference -> Project tab) or a file reference (via Add Reference -> Browse tab)?

    Can you check that Copy Local is set to true:

    1. In Solution Explorer, expand the References node of the project that has a reference to the above assembly
    2. Right-click the assembly and choose Properties
    3. In the Properties window, change Copy Local to True

    Repeat this for each project that has a reference to the assembly.

    Regards

    David

    Saturday, October 28, 2006 6:44 PM
  • I'm not sure why this happens, but try adding these two lines to the "Pre-build event command line" in the MyProjectXXX properties (the line break before "move" has been added by the forum):

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

    Monday, October 30, 2006 10:39 AM
  • Great, the pre-build event fixed it!  Thanks.  Now what exactly is that command line doing?

    PS.  All references were done using project references (not file browse) and copylocal = true.

    Wednesday, November 01, 2006 10:30 PM
  • Service Pack 1 of Visual Studio 2003 said it removed "cannot copy assembly" error.
    http://support.microsoft.com/default.aspx/kb/918007

    I was expecting this error not appear in Visual Studio 2005.
    Wednesday, November 01, 2006 10:37 PM
  • I'll be totally honest: I'm not exactly sure. I'm just passing on something that was passed on to me. My best guess is that the second line renames the -- potentially locked -- Target file, adding a .locked extension, so that the new Target file can be created. The first line just removes the existing .locked copy of the file which was created the last time the pre-build event was run.

    It works, though, and that's the main thing.

    Thursday, November 02, 2006 10:16 AM
  • Hi there,

    I just fix the same problem, but it is a long story, so hope u donot get bored.

    My solution have about 10 projects, the core project use all the others. After some operation to SVN, I cannot build my core project but all the other are ok.

    I use task manager find that once i load the solution, the core project's  xxx.vshost.exe is running. and it restart after manually kill the task.  then i look into my project, some usercontrols and forms cannot be open with designer, so here are the operations i fix this first. u may have one or all of them :)

        1. In a usercontrol class's  .resx file , i find a automatically generated XML code for one of the  properties i defined in .cs file. it is designed for get info at runtime. however, the IDE properties try to read it at design time, so it fail to load the usercontrol.  

    i delete the associated xml code from .resx, and it works well now.

        2. similarly, in the On_paint funtion of my usercontrol, i use a runtime class reference. so at the design time, when IDE load and try to show the usercontrol, and fail.

    so i put a try{} cath{} to go around this, and it works well now.

    after fix this two problems. the xxx.vshost.exe stop running. then my solution recovered from the error of

    "Unable to copy file ..... "

    I think the error is caused by the running xxx.host.exe, it occupies the .dll files so they cannot be replaced at the building time.

     

    Hope this can help

     

     

     

     

    Saturday, November 11, 2006 4:54 AM
  • Thanx a lot! You rule!

    This was happening to me also, on a Visual Studio Add-in project, and your
    solution solved it!

    Every time I built and started the add-in, it would work, but if I wanted to
    rebuild the project again and test it -- the dll would have been locked
    and I would have to restart VS :-/

    Thanx again!

    Thursday, November 16, 2006 8:25 PM
  • This is just a reference problem. That file is holded by some project as a file reference. Even when you don't do that, some time happends to change from project to file reference. Of course not by VS itself, sometimes we just remove some project from solution, then all projects holding reference from that project, it's posible to use the already copied dll in debug folder and change it to file reference. When you have such problem you should find that dll that is holded as file reference and find the project that is having that reference. Finding this is hard, but here is my procedure for that.
    1. Clean the solution in Debug configuration
    2. Change the configuration to Realease and clean the solution again
    3. Start searching dll.* under the solution folder
    4. You will see only the dll that is making you problems, why, because Clean deletes all project reference dll-s but not the file referenced
    5. Delete those dll from search window
    6. Build the solution
    7. Fix the failed project that can't find file reference. Remove missing reference and add it as a project reference.

    I hope this will help you.
    Friday, November 17, 2006 12:40 AM
  • Nice one!

    boban.s's instructions led me to find some designers that I had open in the editor.. specifically settings and resources designers.. closing them before build and run solved this problem for me.

    --kg
    • Proposed as answer by baonguyen_08 Thursday, May 26, 2011 9:00 AM
    • Unproposed as answer by baonguyen_08 Thursday, May 26, 2011 9:00 AM
    Wednesday, January 10, 2007 11:50 AM