Cannot create/shadow copy ‘File Name’ when that file already exists


The two common errors are

  1. Cannot create/shadow copy ‘File Name’ when that file already exists.

    Usually get this message usually when in a debug/edit/debug cycle after successfully building a C# solution and pressing F5 to debug it.

    Exception Details: System.IO.FileLoadException: Cannot create/shadow copy ‘File Name’ when that file already exists.

    If you wait a few moments before pressing F5 after a build, ASP.NET seems to have the time to complete whatever it needs to do. This is running Visual Studio 2005/ASP.NET on localhost.

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

    Workarounds

    The best solution is to do the following, since VS basically locks the file and you cannot use third party resources to unlock it. Just use VS! In the Properties of a project in your IDE you have Build Events. Basically, you can write scripts during pre and post builds of a project. Add these lines in the pre-build event command line, which basically unlocks the DLL within Visual Studio.

     IF EXIST $(TargetPath).LOCKED  (del $(TargetPath).LOCKED)
      ELSE (IF EXIST $(TargetPath) (move $(TargetPath) $(TargetPath).LOCKED))

    Re-compiled and your unable to copy DLL error will not occur.

    More workarounds can be found on:

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

Comments (15)

  1. SVK says:

    in projects i clicked properties wherein i got a window iwth application , compiler etc

    where do i write the above code lines inthe application

  2. Nick says:

    we need some fresh meat on this subject.  it still happens in my case, and i cant seem to find any good posts on the subject.

  3. Nick,

    Can you give me more of what happens?  Are others having this issue as well?

  4. Russ says:

    I usually just do a rebuild and then start the app

  5. Joshua says:

    Basically this does not work… :(.

    Error 4 The command "IF EXIST c:inetpubwwwrootehp2.2WebbinEHP.Web.dll.LOCKED  (del c:inetpubwwwrootehp2.2WebbinEHP.Web.dll.LOCKED)

     ELSE (IF EXIST c:inetpubwwwrootehp2.2WebbinEHP.Web.dll (move c:inetpubwwwrootehp2.2WebbinEHP.Web.dll c:inetpubwwwrootehp2.2WebbinEHP.Web.dll.LOCKED))" exited with code 9009. Web

  6. Xitij Parmar says:

    As i write above two line in my project, i got below error. i'm working in VS 2005 –

    Error: The command "IF EXIST E:DotNetProjects2005OSSCard_OLDDoneCardbinDoneCard.dll.LOCKED  (del E:DotNetProjects2005OSSCard_OLDDoneCardbinDoneCard.dll.LOCKED)

     ELSE (IF EXIST E:DotNetProjects2005OSSCard_OLDDoneCardbinDoneCard.dll (move E:DotNetProjects2005OSSCard_OLDDoneCardbinDoneCard.dll E:DotNetProjects2005OSSCard_OLDDoneCardbinDoneCard.dll.LOCKED))" exited with code 9009.

  7. Zuba Dalama says:

    This error occured when I enabled ASP.NET health monitoring/web events (Site hosted in IIS Express).

  8. Balaji says:

    Check the folder or files has proper permission, if it is not give write permission

  9. Vipin Kumar says:

    Just kill the process w3wp.exe or aspnet_wp.exe from task manager and refresh the page again.

  10. tinadony says:

    I highly recommend to download "Long Path Tool"  useful in situations where you see these error messages:

    Cannot read from source file or disk, there has been a sharing violation, cannot delete file or folder, the file name you specified is not valid or too long, the source or destination file may be in use and many other file managing errors.

  11. Eric Witkowski says:

    For any who encounter the "exited with code 9009" error, its because the code provided above is broken into 2 lines, you want the Build Event Pre-Build value to exist all on one line.  If "Else" is on it's own line, it's treated as its own command, and the compiler gets angry.

    Also, make sure there aren't any "extra" spaces in the line after pasting it in.. this these happen when you paste from a website.

    Hope this helps!

  12. Thanks Eric, that is a great callout.  I have it like that for readability on the web but you are correct that is should be on one line.

  13. garrydam says:

    I just download and use "Long Path Tool" guarantees to fix your error fast and easy. Try it.

  14. Doug says:

    Why can't you guys fix this bug?  It happens to me every single day, wastes my time and slows me down, and has happened since Visual Studio 2008.