Command copy exited with code 4 when building - Vi

2019-01-16 03:59发布

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.

This is because of the post-build event not being able to go through.

Here's what solves the problem, temporarily

  • Sometimes: A restart of Visual Studio and I'm able to build the solution
  • Sometimes: Both a restart of Visual Studio and my file manager of choice (Q-Dir 4.37) solves it.

Here's what the post-build event looks like:

xcopy "$(SolutionDir)Solution Items\References\*.dll" "$(TargetDir)" /Y

When you get the command copy exited with code [insert value] error, it's normally because of the following:

  • read / write permissions
  • missing files
  • wrong directories

However - obviously at times when I build the solution, there's no problem.

FYI, I uninstalled ReSharper 5.1.1 two weeks ago and Visual Studio's been giving me some errors since then (among them not being able to debug). I re-installed Visual Studio and it's working better since then, but still get this problem. Could it have to do with some ReSharper stuff being somewhere?

Have you had the same problem and solved it? Or do you have any possible solution to it?

Cheers.

25条回答
萌系小妹纸
2楼-- · 2019-01-16 04:43

I had the same issue. It was caused by having the same flag twice, for example:

if $(ConfigurationName) == Release (xcopy "$(TargetDir)." "$(SolutionDir)Deployment\$(ProjectName)\" /e /d /i /y /e)

Observe that the "/e" flag appears twice. Removing the duplicate solved the issue.

查看更多
别忘想泡老子
3楼-- · 2019-01-16 04:45

I had the same problem. However, nothing worked for me. I solved the issue by adding

exit 0

to my code. The problem was that while I was doing copying of the files, sometimes the last file could not be found, and the bat returned a non-zero value.

Hope this helps someone!

查看更多
放我归山
4楼-- · 2019-01-16 04:45

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.

查看更多
放我归山
5楼-- · 2019-01-16 04:46

I got this error because of the file was opened in another instance.

when i closed the file and again re-build the solution, it was successfully copied.

查看更多
相关推荐>>
6楼-- · 2019-01-16 04:46

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.

查看更多
虎瘦雄心在
7楼-- · 2019-01-16 04:47

Run VS in Administrator mode and it should work fine.

查看更多
登录 后发表回答