programing tip

빌드시 코드 4로 명령 복사가 종료되었습니다.-Visual Studio를 다시 시작하면 문제가 해결됩니다.

itbloger 2020. 6. 14. 11:03
반응형

빌드시 코드 4로 명령 복사가 종료되었습니다.-Visual Studio를 다시 시작하면 문제가 해결됩니다.


때때로 여기에 솔루션을 빌드 할 때 (7 개의 프로젝트가 있음) Visual Studio 2010 Premium ed에서``코드 4로 종료 된 명령 사본 ''오류가 발생합니다.

이는 빌드 후 이벤트가 진행되지 않기 때문입니다.

일시적으로 문제를 해결하는 방법은 다음과 같습니다.

  • 때때로 : Visual Studio를 다시 시작하면 솔루션을 빌드 할 수 있습니다.
  • 때때로 : Visual Studio를 다시 시작하고 선택한 파일 관리자 (Q-Dir 4.37)가 문제를 해결합니다.

빌드 후 이벤트는 다음과 같습니다.

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

코드 [삽입 값] 오류와 함께 명령 사본이 종료되면 일반적으로 다음과 같은 원인이 있습니다.

  • 읽기 / 쓰기 권한
  • 누락 된 파일
  • 잘못된 디렉토리

그러나 분명히 솔루션을 빌드 할 때 아무런 문제가 없습니다.

참고로, 나는 2 주 전에 ReSharper 5.1.1을 제거했으며 그 이후로 Visual Studio에서 오류가 발생했습니다 (디버그 할 수 없음). Visual Studio를 다시 설치했지만 그 이후로는 더 잘 작동하지만 여전히이 문제가 발생합니다. ReSharper와 관련이있을 수 있습니까?

같은 문제가 있고 해결 했습니까? 아니면 가능한 해결책이 있습니까?


나는 이것이 파일 잠금 문제라는 것을 항상 발견했다. 코드 4는 파일에 액세스 할 수 없습니다. 내가 찾은 부분 해결책 중 하나는 xcopy에 / C 옵션을 사용하는 것입니다 (오류가 계속 발생합니다). 실제로 해결책은 아니지만 대부분 빌드가 실패하는 것을 막았습니다.

32 비트에서만 작동하는 또 다른 솔루션은 잠금 해제 도구를 사용 하여 복사하기 전에 파일의 창 핸들을 해제하는 것입니다.

편집 : 64 비트에서도 작동한다는 것을 깨달았습니다.


/C오류를 무시할 수는 있지만 빌드가 성공하려면 반드시 복사해야하는 파일이있을 수 있으므로 실제 솔루션이 아닐 수도 있습니다.

가장 일반적인 문제는 사전 정의 된 명령 태그 (예 :) 주위에 누락 된 따옴표 $TargetDir입니다. 코드 나 TFS에서 다양한 브랜치와 경로를 만들면 이런 일이 발생할 가능성이 매우 높습니다.

파일이 읽기 전용 인 경우에도 문제가 발생할 수 있습니다. /R읽기 전용 파일을 복사 할 수 있는 옵션을 추가하십시오 . 사용 가능한 옵션 목록은 다음에서 찾을 수 있습니다.

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true

또 다른 가능한 문제는 기본 폴더에 액세스 할 수 없다는 것입니다. 그렇다면 "start xcopy"대신 대신 수행해보십시오 "xcopy". 관리자 권한이있는 다른 명령 창이 열립니다.


나는 같은 오류를 겪었지만 파일이 잠겨 있지 않기 때문에 파일이 없습니다.

VS가 기존 파일이 아닌 파일을 복사하려고 시도한 이유는 빌드 후 이벤트 명령 때문입니다.

내가 그것을 정리 한 후 문제가 해결되었습니다.

최신 정보:

@rhughes는 다음과 같이 언급했습니다.

실제 문제는 명령을 제거하는 대신 여기에서 명령을 수행하는 방법입니다.

그리고 그는 절대적으로 옳습니다.

여기에 이미지 설명을 입력하십시오


나는 또한이 문제에 직면했다. 오류 창에서 결과를 두 번 확인하십시오.

제 경우에는 꼬리 \가 xcopy와 충돌했습니다 (을 사용하는 것처럼 $(TargetDir)). 내 경우에는 $(SolutionDir)..\bin. 다른 출력을 사용하는 경우 조정해야합니다.

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.


In case the post build event contains copy/xcopy command for copying build output to some directory(which usually is the most common post build operation) the problem can occur in case the full directory path either of source or target destinations contain folder names which include spaces. Remove space for the directory name(s) and try.


As mentioned in many sites, there are various reasons for this. For me it was due to the length of Source and Destination (Path length). I tried xcopy in the command prompt and I was unable to type the complete source and path (after some characters it wont allow you to type). I then reduced the path length and was able to run. Hope this helps.


I got this error because the user account that TFS Build Service was running under did not have permissions to write to the destination folder. Right-click on the folder-->Properties-->Security.


Run VS in Administrator mode and it should work fine.


This can happen in multiple cases:

  1. When the complete string path is longer than 254 chars.
  2. When the name of the file to be copied is wrong.
  3. When the target path is wrong.
  4. When the readonly attribute is set on the copied file or target folder.

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.


I faced the same issue in case of XCOPY after build is done. In my case the issue was happening because of READ-ONLY permissions set on folders.

I added attrib -R command before XCOPY and it solved the issue.

Hope it helps someone!


I had the same error with xcopy in connection with the Test Engine. I am using VisualStudio Professional 2013. By default Test -> Test Settings -> Keep Test Execution Engine Running seems to be the reason for my error code 4 with xcopy. Switching it off solved the problem. The execution engine seems to keep hold on some .dlls.


I had the same problem. A simple 'Clean Solution' in VS cleared the error, but it was a temporary solution.


I found that setting the file's Copy To Output Directory parameter to Copy Always seems to have cleared up the locking issue. Although now I have 2 copies of the files and need to delete one.


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!


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.


I faced same issue. I deleted post-build events and it started working. Some times when we add some SQL components it may add post build commands also.


I am getting something similar using an xcopy with the /exclude option. In my case, I found that editing the post-build event (something harmless like a newline after the command) and saving the project causes the error to happen. Re-saving the file specified in the /exclude option causes it to work again.


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.


What fixed it for me: dig down to the specific solution for the project you want i.e NOT the overall solution file for all the projects.

Do try - I tried everything else mentioned here but to no avail.


I don't see anything in here to suggest that this is a web-app but I have experienced this issue myself - I've got two xcopy commands on a post-build event and only one of them was failing. Something had a lock on the file, and it wasn't Visual Studio (as I tried restarting it.)

The only other thing that would have used the dll I built was IIS. And lo and behold,

A simple iisreset did the trick for me.


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.


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.


Can be caused by VMWare Workstation with Shared Folders

I have the problem always when the destinatinon folder of the xcopy is also mapped as Shared Folder in a VM.

I solved it with a script running in the vm and deleting the content of the shared folder.


To expand on rhughes answer,

The robocopy works beautifully, just incase you need to include sub directories you can use /e to include subs and copy empty directories or /s to include subs excluding empty directories.

Also robocopy will report back a few things like if new files were copied, this will cause VS to complain since anything above 0 is a failure and robocopy will return 1 if new files have been found. Its worth to mention that robocopy first compares the Source/Dest and only copies the updated/new files.

To get around this use:

(robocopy "$(SolutionDir)Solution Items\References\*.dll" "$(TargetDir)") ^& IF %ERRORLEVEL% LEQ 4 exit /B 0

If you are here because your project fails to build on a build server, but builds fine "manually" on a dev machine, and you are doing xcopy only for debugging and to emulate a production environment on a dev machine, then you may want to look at this solution:

https://stackoverflow.com/a/1732478/2279059

You simply turn off post build events on the build server using

msbuild foo.sln /p:PostBuildEvent=

This is not good enough if you have other post build events that also need to run on the build server, and it is not a general solution. However, since there are so many different causes of this problem, there cannot be a general solution. One of the many answers to this question (and its duplicates) will probably help, but be careful with approaches that only somehow circumvent error handling (such as xcopy /C). Those may work for you, particularly also in the build server scenario, but I think this one is more reliable, IF it can be used.

It has also been suggested that with newer versions of Visual Studio, the problem no longer exists, so if you are using an old version, consider updating your build tools.


Error code 4 can mean a lot of things, so I recommend reading the other answers as well until you find a solution that works for you AND you understand WHY it works (some solutions only disable error handling, which may only mask the problem but not solve it).

This can be a file locking issue related to parallel building. A workaround is to not use parallel building. This is the default behavior, but if you are using the -m option, then projects will be built in parallel. The following variations should not build projects in parallel, so you will not run into the file locking problem.

msbuild -m:1
msbuild -maxcpucount:1
msbuild

Note that, contrary to what has been said here, this even happens with the "latest" version of MSBuild (from Build Tools for Visual Studio 2019).

최상의 해결책은 빌드 후 단계에서 파일을 복사 할 필요가 없도록하는 것입니다. 경우에 따라 빌드 서버에서 MSBuild로 빌드 할 때 빌드 후 단계를 비활성화 할 수도 있습니다. https://stackoverflow.com/a/55899347/2279059

참고 URL : https://stackoverflow.com/questions/3861474/command-copy-exited-with-code-4-when-building-visual-studio-restart-solves-it

반응형