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:

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