locked
"Unable to copy file The process cannot access the file because it is being used by another process" visual studio 2005 compile

    Question

  • Hi All,

    I hope someone can help me with these very frustrating issue.

     

    When I load my solution and compile it after first starting the IDE, everything works as expected. After changing source and recompiling, I get the following compile error:

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

     

    After closing Visual Studio 2005 and reopening the solution I can compile again successfully.  This is an old solution that has been fine for a long time.  I just recently changed computers and reinstalled everything.

     

    I am running win XP Pro sp2, Visual Studio 2005 sp1.

     

    Does anyone have any ideas? 

     

    Thanks,

     

    Craig Adams

    Friday, September 21, 2007 6:03 PM

Answers

  • Craig Adams,

     

    Based on your post, there is error message "Unable to copy file. The process access the file because it is being used by another process." This problem is quite common in MSDN forums with the same class library reference. I would like to provide you the suggestions as follows:

     

    1. The problem always happens on running when you try to click Debug to rebuild the application. In your description, you have closed the application, however, the problem remains. Sometimes, when there is Form and you close the application by closing the Form UI. This may not close the application and it is better to use Task Manager or ProcessMonitor to see whether the application is really finished.

     

    2. Please try to dispose each of the Object created in your application when you try to finish. You can add the AppClose subroutine to dispose all the Objects, then call the Sub in your close event.

     

    3. You can also take a look at the related threads with the solutions provided:

     

    Unable to copy from obj\debug to bin\debug

     

    I've found out how to sort it out. I ran a program which figured out which application had the file locked, and it turned out to be devenv.exe, i.e. Visual Studio itself. A bit more searching and I found a fix.

    Create a pre-build action in your project by going to project properties (right-click on the project in the solution explorer, and select the Properties option), select the Build Events tab. Add this code:

    if exist "$(TargetPath).locked" del "$(TargetPath).locked"
    if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
    This copies the file to a different name, and allows the build to continue successfully.

     

    Error 5 Unable to copy file "obj\Debug\BO.dll" to "bin\Debug\BO.dll". The process cannot access the file 'bin\Debug\BO.dll' ...

     

    Hope that can help you.

    Tuesday, September 25, 2007 6:53 AM

All replies

  • Craig,

    It looks like the project is a class library, what type of project is calling it?

    Thanks,


    Steve - dotneticated.com
    Monday, September 24, 2007 2:23 AM
  • Hi Steve,

    The project that is calling it is a windows forms exe.  Yes, the project is a class library with the issue.  I am referencing the project from my exe and that same class library is being reference from two other class libraries. In each of them, I am referencing the class library as a project and not a dll.

     

    Thanks,

     

    Craig

     

    Vivus Software Inc

     

     

    Monday, September 24, 2007 2:15 PM
  • Craig Adams,

     

    Based on your post, there is error message "Unable to copy file. The process access the file because it is being used by another process." This problem is quite common in MSDN forums with the same class library reference. I would like to provide you the suggestions as follows:

     

    1. The problem always happens on running when you try to click Debug to rebuild the application. In your description, you have closed the application, however, the problem remains. Sometimes, when there is Form and you close the application by closing the Form UI. This may not close the application and it is better to use Task Manager or ProcessMonitor to see whether the application is really finished.

     

    2. Please try to dispose each of the Object created in your application when you try to finish. You can add the AppClose subroutine to dispose all the Objects, then call the Sub in your close event.

     

    3. You can also take a look at the related threads with the solutions provided:

     

    Unable to copy from obj\debug to bin\debug

     

    I've found out how to sort it out. I ran a program which figured out which application had the file locked, and it turned out to be devenv.exe, i.e. Visual Studio itself. A bit more searching and I found a fix.

    Create a pre-build action in your project by going to project properties (right-click on the project in the solution explorer, and select the Properties option), select the Build Events tab. Add this code:

    if exist "$(TargetPath).locked" del "$(TargetPath).locked"
    if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
    This copies the file to a different name, and allows the build to continue successfully.

     

    Error 5 Unable to copy file "obj\Debug\BO.dll" to "bin\Debug\BO.dll". The process cannot access the file 'bin\Debug\BO.dll' ...

     

    Hope that can help you.

    Tuesday, September 25, 2007 6:53 AM
  • Bruno,

    Thanks for taking the time to answer my post. Your solution seems to have taken care of my problem.  I will check out the links for additional info.

     

    I still am not clear why my dll is being locked or why Visual Studio is hanging on to it but I am prepared to live with that mystery in the interest of moving forward.

     

    Thanks again, you are the man,

     

    Craig Adams

     

    Vivus Software Inc

     

    Tuesday, September 25, 2007 6:23 PM
  • Check out this post:

    http://connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID=114592

    I deleted the .resx file and my problem was solved. Of course, this may not work for everybody - but just thought I'd mention...

    Tuesday, November 13, 2007 4:11 AM
  • Thanks Bruno.. Pre build command  worked Smile

    Friday, March 28, 2008 6:28 PM
  •  

    yes it worked for me too..

     

    Thanks Bruno.

    Monday, May 12, 2008 11:38 AM
  • Bruno, you are the life saver.
    The pre-build command works!
    I'v struggled with this issue couple days.
    Thank you.
    Monday, August 04, 2008 1:51 PM
  • I'm working on a VS2005 addin and had the "unable to copy issue". The pre-build command works great.  Thank you very much Bruno!
    Thursday, October 02, 2008 5:31 AM
  • Thanks Bruno!  Struggled with this one for a long time till I found your pre-build code...works great!
    • Proposed as answer by Doug Clutter Wednesday, December 10, 2008 1:56 AM
    Wednesday, December 10, 2008 1:55 AM
  • I had this, and I traced it to somehow getting my forms exe added in my project references.

    I think it happened when I tried to add a user control to a form with performing a build.  I got an error message, and everything was bad from there...

    Maybe it tried to add my exe to my projects references. 

    Could have been something else...

    I was also getting messages that the class "MyClass" in my exe had a version conflict with the source code version...

    Bob
    Tuesday, February 17, 2009 5:55 AM
  • Actually your solution doesn't work because the same thing happens to the locked file eventually.  Here is how to append a date/time stamp onto the renamed file and delete using a wildcard

    if exist "$(TargetPath)"*".locked" del "$(TargetPath)"*".locked"
    if exist "$(TargetPath)" move "$(TargetPath)" "$(TargetPath)."%date:~4,2%%date:~7,2%%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.locked

    I found this works great . . .
    Thursday, July 30, 2009 5:08 PM
  • We are having the same exact problem. The solution above does not solve it. I created two simpler VS Solutions that duplicated the problem.

    Environment:
    • OS - WinXP SP3 with all the latest updates.
    • Visual Studio 2008 with all the latest updates.
    Solutions:
    1. Open Visual Studio and create a VB Class Library project. Add the following property and property method:

             Protected mProperty1 As String

             Public Property Property1() As String
              Get
                  Return mProperty1
              End Get
              Set(ByVal pProperty1 As String)
                  mProperty1 = Left(pProperty1, 100)
              End Set
          End Property
    2. Compile the project.
    3. Open a second copy of Visual Studio and create a VB Windows Application project.
    4. Add a reference to \Projects\ClassLibrary1\bin\debug\ClassLibrary1.dll
    5. Compile the project.
    6. Alt-tab to the Class Library project and change the Left(pProperty1, 100) to Left(pProperty1, 50).
    7. Compile the project.
    8. Alt-tab to the Windows Application project.
    9. The error list shows two errors:

      Error    1    Unable to load referenced library 'D:\Visual Studio 2008\Projects\ClassLibrary1\bin\Debug\ClassLibrary1.dll': The process cannot access the file because it is being used by another process.   

      Error    2    Unable to load referenced library 'D:\Visual Studio 2008\Projects\ClassLibrary1\bin\Debug\ClassLibrary1.dll': The process cannot access the file because it is being used by another process.    D:\Visual Studio 2008\Projects\ClassLibrary1\bin\Debug\ClassLibrary1.dll   
    10. If I close the Windows Application project (leaving Visual Studio open) and re-open it, the error goes away and the project compiles.
    Tuesday, October 06, 2009 11:15 PM
  • I've been having the same problem and am very intersted in a solution or work around.
    Wednesday, October 07, 2009 6:39 PM
  • I've also been having this issue and none of the suggested workarounds have worked for my problem.  If I follow the same process as Joe Sweeney, I receive am able to create the same issue in my environment.  I am in the same environment also (VS 2008 / WIN XP SP3).  Any help would be appreciated as this is an extremely frustrating bug.
    Monday, October 12, 2009 6:54 PM
  • Thanks Craig, This worked for me. I was really frustrated with the Visual studio creating this problem for my build and adding the macros to the pre-build event worked.

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


    Thanks

    Abish Asharaf
    Friday, October 23, 2009 9:49 AM
  • The build scripts didn't work for me, but if I delete the file from References and add it back, it works.

    Totally frustrating.
    Friday, December 04, 2009 10:16 PM
  • I had this problem.  Had to set the "Copy Local" property to false in the reference properties for the dll.  Now it works.
    • Proposed as answer by Klaus W Thursday, March 18, 2010 10:23 AM
    Friday, December 18, 2009 7:14 PM
  • Absolutely Correct working after trying this, Thanks for the solution.
    Thursday, February 18, 2010 3:50 PM
  • Had this problem forever and often was forced to reboot the machine completely in order ot unlock the Solution and be able to compile again.

    [quote]
    if exist "$(TargetPath).locked" del "$(TargetPath).locked"
    if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
    [/quote]

    doesn't work for me, I get an Exit code 1 message.

    [quote]
    if exist "$(TargetPath)"*".locked" del "$(TargetPath)"*".locked"
    if exist "$(TargetPath)" move "$(TargetPath)" "$(TargetPath)."%date:~4,2%%date:~7,2%%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.locked
    [/quote]

    worked once and got me my solution unlocked at first compile 
    No error codes on this script, but just the original "unable to copy ..." came back on second attempt to recompile.

    Removing "Copy Local' in all projects (apart form the main project) of the solution unlocked the solution and I have compiled multiple times now. Problem seems solved with this.

    Running System which had issues: Windows 7 and Visual Studio 2008.
    • Edited by Klaus W Thursday, March 18, 2010 10:33 AM spelling error
    • Proposed as answer by LuisF77 Wednesday, May 19, 2010 3:45 AM
    Thursday, March 18, 2010 10:31 AM
  • I also had the same issue. I took the following path.

    1) Project -> Right Click -> Properties -> Build - > Output -> Output Path : bin\

    2) Deleted the folder "obj"

    3) Build The project.

    The error is gone.

    Thursday, May 20, 2010 3:37 PM