Every now and then when I build my solution here (with 7 projects in it) I get the dreaded \'Command copy exited with code 4\' error, in Visual Studio 2010 Premium ed.
I've invariably found this to be a file locking issue. Code 4 is Cannot Access File. One partial solution I found is to use the /C option for xcopy (which continues on error). Not really a solution but mostly it has stopped my builds from failing.
Another solution which only works on 32 bit is to use the unlocker tool to release the windows handles on the file before the copy.
Edit: I've just realised that it works under 64 bits too.
As I am writing a DLL library I used the xcopy command to copy the library where the program can find and load it. After several times of opening and closing the program there was still an open process of it in taskmanager which i did not recognized.
Look for any process from which the file may be used and close it.
I had the same problem. A simple 'Clean Solution' in VS cleared the error, but it was a temporary solution.
If you are running Windows 7 onward, you can try the new 'robocopy' command:
robocopy "$(SolutionDir)Solution Items\References\*.dll" "$(TargetDir)"
More information about robocopy can be found here.
In my case my $(OutDir)
was simply ..\..\Build\
i.e. some relative path. And, when I was trying to xcopy as follows
xcopy /y "$(OutDir)Proj1.dll" "Anypath\anyfolder\"
I was getting the exit code error 4.
What's happening was, this command was getting executed in the $(OutDir) (in my case build folder) itself and not the directory where the csproj file of the project was located (as we would normally expect). Hence, I kept getting File not found
error (corresponding to exit code 4).
I couldn't figure this out until I wrote cd
in the Post Build events, to print which directory this was getting executed in.
So, to summarize, if we're wishing to copy
/ xcopy
files from the $(OutDir)
, either use "$(TargetDir)"
(which is complete path for output directory) or need not specify any path at all.
I have also faced this problem.Double check the result in the error window.
In my case, a tailing \
was crashing xcopy (as I was using $(TargetDir)
). In my case $(SolutionDir)..\bin
. If you're using any other output, this needs to be adjusted.
Also note that start xcopy
does not fix it, if the error is gone after compiling. It might have just been suppressed by the command line and no file has actually been copied!
You can btw manually execute your xcopy commands in a command shell. You will get more details when executing them there, pointing you in the right direction.