Cannot create/shadow copy 'File Name' when that file already exists
The two common errors are
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.
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
Comments
Anonymous
June 09, 2008
PingBack from http://blogs.msdn.com/tom/archive/2008/06/09/cannot-create-shadow-copy-file-name-when-that-file-already-exists.aspxAnonymous
May 31, 2009
in projects i clicked properties wherein i got a window iwth application , compiler etc where do i write the above code lines inthe applicationAnonymous
January 08, 2010
The comment has been removedAnonymous
February 22, 2010
Nick, Can you give me more of what happens? Are others having this issue as well?Anonymous
March 12, 2010
I usually just do a rebuild and then start the appAnonymous
May 14, 2010
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. WebAnonymous
February 13, 2011
The comment has been removedAnonymous
October 16, 2011
This error occured when I enabled ASP.NET health monitoring/web events (Site hosted in IIS Express).Anonymous
March 06, 2012
Check the folder or files has proper permission, if it is not give write permissionAnonymous
August 07, 2012
The comment has been removedAnonymous
August 02, 2013
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.Anonymous
August 21, 2013
The comment has been removedAnonymous
August 21, 2013
The comment has been removedAnonymous
August 27, 2013
I just download and use "Long Path Tool" guarantees to fix your error fast and easy. Try it.Anonymous
October 27, 2013
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.