构建时命令复制退出并显示代码 4 - Visual Studio 重新启动解决了该问题

发布于 2024-09-26 14:01:22 字数 682 浏览 1 评论 0原文

当我在这里构建解决方案(其中包含 7 个项目)时,我会时不时地在 Visual Studio 2010 Premium 版中收到可怕的“命令复制已退出,代码为 4”错误。

这是因为构建后事件无法完成。

以下是暂时解决问题的方法

  • 有时:重新启动 Visual Studio,我就能够构建解决方案
  • 有时:重新启动 Visual Studio 和我选择的文件管理器 (Q-Dir 4.37) 都可以解决该问题。

构建后事件如下所示:

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

当您收到命令副本退出并出现代码 [插入值] 错误时,通常是由于以下原因:

  • 读/写权限
  • 缺少文件
  • 错误的目录

但是 - 显然有时当我构建解决方案时,没问题。

仅供参考,我两周前卸载了 ReSharper 5.1.1,从那时起 Visual Studio 一直给我一些错误(其中无法调试)。我重新安装了 Visual Studio,从那时起它工作得更好,但仍然遇到这个问题。这是否与某些 ReSharper 东西有关?

你有遇到同样的问题并解决了吗?或者你有什么可能的解决方案吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(28

北城挽邺 2024-10-03 14:01:22

虽然 /C 可能会忽略错误,但它可能不是真正的解决方案,因为可能必须复制某些文件才能成功构建。

最常见的问题是预定义命令标记周围缺少引号(例如 $TargetDir)。当人们在代码或 TFS 中创建各种分支和路径时,发生这种情况的可能性非常高。

有时,如果文件是只读的,也会导致问题。添加 /R 选项以允许复制只读文件。您可以在以下位置找到可用选项的列表:

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

另一个可能的问题是基础文件夹无法被访问。如果是这样,请尝试执行“start xcopy”而不是“xcopy”。这将打开另一个命令窗口,但具有管理员权限。

While /C may ignore errors, it might not be the real solution as there could be files that MUST be copied in order for the build to be successful.

The most common issue is the missing quotes around the pre-defined command tags (such as $TargetDir). When one creates various branches and paths in code or TFS, there is a very high chance for this to occur.

Sometimes if the file is read only, it will cause issues too. Add the /R option to allow read only files to be copied over. You can find list of available options at:

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

Another possible issue is that the underlying folder cannot be accessed. If so, trying performing "start xcopy" instead of "xcopy". This will open another command window but with admin priveleges.

永不分离 2024-10-03 14:01:22

我总是发现这是一个文件锁定问题。代码 4 表示无法访问文件。我发现的一个部分解决方案是使用 xcopy 的 /C 选项(出错时继续)。这并不是一个真正的解决方案,但主要是它阻止了我的构建失败。

另一种仅适用于 32 位的解决方案是使用 unlocker 工具来释放文件上的窗口句柄复制之前。

编辑:我刚刚意识到它也可以在 64 位下运行。

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.

瞄了个咪的 2024-10-03 14:01:22

我也遇到了同样的错误,但不是由于文件被锁定,而是文件丢失。

VS 尝试复制不存在的文件的原因是由于 Post-build event 命令。

我清除后,问题就解决了。

更新:

正如@rhughes 评论的那样:

真正的问题是如何让命令在这里工作,而不是
删除它。

他是绝对正确的。

在此处输入图像描述

I crossed the same error, but it is not due to the file is locked, but the file is missing.

The reason why VS tried to copy an not existing file, is because of the Post-build event command.

After I cleared that, problem solved.

UPDATE:

As @rhughes commented:

The real issue is how to get the command here to work, rather than to
remove it.

and he is absolutely right.

enter image description here

煞人兵器 2024-10-03 14:01:22

我也遇到过这个问题。请仔细检查错误窗口中的结果。

就我而言,拖尾 \ 导致 xcopy 崩溃(因为我使用的是 $(TargetDir))。就我而言 $(SolutionDir)..\bin。如果您使用任何其他输出,则需要进行调整。

另请注意,如果编译后错误消失,start xcopy 不会修复它。它可能只是被命令行抑制并且实际上没有复制任何文件!

顺便说一句,您可以在命令 shell 中手动执行 xcopy 命令。在那里执行它们时,您将获得更多详细信息,为您指明正确的方向。

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.

北凤男飞 2024-10-03 14:01:22

如果构建后事件包含用于将构建输出复制到某个目录的 copy/xcopy 命令(这通常是最常见的构建后操作),则如果源或目标目标的完整目录路径包含文件夹名称,则可能会出现问题空间。删除目录名称的空格并尝试。

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.

无需解释 2024-10-03 14:01:22

正如许多网站所提到的,造成这种情况的原因有多种。对我来说,这是由于源和目的地的长度(路径长度)造成的。我在命令提示符下尝试了 xcopy,但无法输入完整的源代码和路径(在某些字符之后,它不允许您输入)。然后我减少了路径长度并能够运行。
希望这有帮助。

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.

初雪 2024-10-03 14:01:22

这可能在多种情况下发生:

  1. 当完整的字符串路径超过 254 个字符时。
  2. 当要复制的文件名错误时。
  3. 当目标路径错误时。
  4. 当复制的文件或目标文件夹设置只读属性时。

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.
ゝ杯具 2024-10-03 14:01:22

我收到此错误是因为运行 TFS 生成服务的用户帐户没有写入目标文件夹的权限。 右键单击该文件夹-->属性-->安全

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.

ぺ禁宫浮华殁 2024-10-03 14:01:22

在管理员模式下运行VS,它应该可以正常工作。

Run VS in Administrator mode and it should work fine.

用心笑 2024-10-03 14:01:22

我收到此错误是因为该文件是在另一个实例中打开的。

当我关闭文件并再次重新构建解决方案时,它已成功复制。

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.

只涨不跌 2024-10-03 14:01:22

在构建完成后进行 XCOPY 时,我遇到了同样的问题。就我而言,问题是由于文件夹上设置的只读权限而发生的。

我在 XCOPY 之前添加了 attrib -R 命令,它解决了问题。

希望它能帮助别人!

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!

漫雪独思 2024-10-03 14:01:22

我在与测试引擎相关的 xcopy 中遇到了同样的错误。我使用的是 VisualStudio Professional 2013。默认情况下测试 ->测试设置->保持测试执行引擎运行似乎是我使用 xcopy 时出现错误代码 4 的原因。将其关闭就解决了问题。执行引擎似乎保留了一些 .dll。

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.

§普罗旺斯的薰衣草 2024-10-03 14:01:22

我也有同样的问题。
VS 中的一个简单的“干净解决方案”清除了错误,但这只是一个临时解决方案。

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

浮世清欢 2024-10-03 14:01:22

我发现将文件的“复制到输出目录”参数设置为“始终复制”似乎已经解决了锁定问题。虽然现在我有 2 个文件副本,需要删除一份。

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.

不美如何 2024-10-03 14:01:22

我也有同样的问题。然而,对我来说没有任何作用。我通过添加到我的代码中解决了这个问题

exit 0

。问题是,当我复制文件时,有时找不到最后一个文件,并且bat返回一个非零值。

希望这对某人有帮助!

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!

·深蓝 2024-10-03 14:01:22

如果您运行的是 Windows 7 及以上版本,则可以尝试新的“robocopy”命令:

robocopy "$(SolutionDir)Solution Items\References\*.dll" "$(TargetDir)"

有关 robocopy 的更多信息,请访问 这里

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.

偏爱自由 2024-10-03 14:01:22

我遇到了同样的问题。
我删除了构建后事件,它开始工作。
有时,当我们添加一些 SQL 组件时,它也可能会添加构建后命令。

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.

〃安静 2024-10-03 14:01:22

我使用带有 /exclude 选项的 xcopy 得到类似的东西。就我而言,我发现编辑构建后事件(命令后的换行符等无害的东西)并保存项目会导致错误发生。重新保存 /exclude 选项中指定的文件会使其再次工作。

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.

帅的被狗咬 2024-10-03 14:01:22

当我编写 DLL 库时,我使用 xcopy 命令将库复制到程序可以找到并加载它的位置。多次打开和关闭程序后,任务管理器中仍然有一个打开的进程,我无法识别。

查找可能使用该文件的任何进程并将其关闭。

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.

眼中杀气 2024-10-03 14:01:22

是什么为我解决了这个问题
深入挖掘您想要的项目的特定解决方案,即不是所有项目的整体解决方案文件。

请尝试 - 我尝试了这里提到的其他所有内容,但没有成功。

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.

千寻… 2024-10-03 14:01:22

我在这里没有看到任何内容表明这是一个网络应用程序,但我自己也遇到过这个问题 - 我在构建后事件上有两个 xcopy 命令,但只有一个失败。有东西锁定了该文件,而且它不是 Visual Studio(当我尝试重新启动它时)。

唯一会使用我构建的 dll 的其他东西是 IIS。你瞧,

一个简单的 iisreset 就帮我解决了这个问题。

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.

决绝 2024-10-03 14:01:22

我有同样的问题。
这是由于两次使用相同的标志引起的,例如:

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

观察“/e”标志出现两次。删除重复项解决了问题。

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.

时光与爱终年不遇 2024-10-03 14:01:22

就我而言,我的 $(OutDir) 只是 ..\..\Build\ 即一些相对路径。而且,当我尝试如下 xcopy 时
xcopy /y "$(OutDir)Proj1.dll" "Anypath\anyfolder\" 我收到退出代码错误 4。

发生的情况是,此命令在 $(OutDir) (在我的例子中,build 文件夹)本身,而不是项目的 csproj 文件所在的目录(正如我们通常期望的那样)。因此,我不断收到找不到文件错误(对应于退出代码 4)。

直到我在构建后事件中写入 cd 来打印正在执行的目录时,我才弄清楚这一点。

所以,总而言之,如果我们希望 copy< /code> / xcopy 来自 $(OutDir) 的文件,或者使用 "$(TargetDir)" (这是输出目录的完整路径)或者根本不需要指定任何路径。

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.

递刀给你 2024-10-03 14:01:22

可能是由具有共享文件夹的 VMWare Workstation 引起的

xcopy 的目标文件夹也映射为虚拟机中的共享文件夹时,我总是遇到问题。

我通过在虚拟机中运行的脚本并删除共享文件夹的内容解决了这个问题。

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.

病女 2024-10-03 14:01:22

为了扩展 rhughes 的答案,

robocopy 工作得很好,万一您需要包含子目录,您可以使用 /e 来包含子目录并复制空目录或 /s 来包含子目录不包括空目录。

此外,robocopy 还会报告一些信息,例如是否复制了新文件,这将导致 VS 抱怨,因为任何高于 0 的值都表示失败,如果找到新文件,robocopy 将返回 1。值得一提的是,robocopy 首先比较源/目标,然后仅复制更新/新文件。

要解决此问题,请使用:

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

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
百合的盛世恋 2024-10-03 14:01:22

如果您在这里是因为您的项目无法在构建服务器上构建,但在开发计算机上“手动”构建良好,并且您执行 xcopy 仅用于调试和在开发设备上模拟生产环境 您可能需要查看以下解决方案:

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

机,那么 只需使用以下方法关闭构建服务器上的构建后事件:

msbuild foo.sln /p:PostBuildEvent=

如果您还有其他也需要在构建服务器上运行的构建后事件,那么这还不够好,而且它不是通用解决方案。然而,由于造成这个问题的原因有很多,所以不可能有一个通用的解决方案。这个问题(及其重复项)的众多答案之一可能会有所帮助,但要小心仅以某种方式规避错误处理的方法(例如 xcopy /C )。这些可能对您有用,特别是在构建服务器场景中,但我认为如果可以使用的话,这个更可靠。

还有人建议,使用较新版本的 Visual Studio,该问题不再存在,因此如果您使用的是旧版本,请考虑更新您的构建工具。

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.

执着的年纪 2024-10-03 14:01:22

错误代码 4 可能意味着很多事情,所以我建议您也阅读其他答案,直到您找到适合您的解决方案并且您了解它为何有效(某些解决方案仅禁用错误处理,这可能只会掩盖问题,但不会解决它)。

这可能是与并行构建相关的文件锁定问题。解决方法是不使用并行构建。这是默认行为,但如果您使用 -m 选项,则项目将并行构建。以下变体不应并行构建项目,因此您不会遇到文件锁定问题。

msbuild -m:1
msbuild -maxcpucount:1
msbuild

请注意,与此处所述相反,即使是“最新”版本的 MSBuild(来自 Visual Studio 2019 的构建工具)也会发生这种情况。

最好的解决方案可能是确保您不需要在构建后步骤中复制文件。在某些情况下,您还可以在生成服务器上使用 MSBuild 进行生成时禁用生成后步骤:https://stackoverflow.com/a /55899347/2279059

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).

The best solution is probably to make sure you don't need to copy files in a post-build step. In some situations, you can also disable post-build steps when building with MSBuild on a build server: https://stackoverflow.com/a/55899347/2279059

揽月 2024-10-03 14:01:22

我想放大和具体化这两个答案:@Vemul's@Srihari Chinna 的

  • 确保您的路径存在并且该进程可以访问它。
    • 如果您使用变量替换来组装源路径,则尤其如此。

I want to amplify and crystallize these two answers: @Vemul's, @Srihari Chinna's.

  • Make sure that your source path exists and that the process has access to it.
    • This is especially true if you're using variable substitution to assemble the source path.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文