visual-studio 构建时命令副本以代码 4 退出 - Visual Studio 重启解决了

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3861474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:32:27  来源:igfitidea点击:

Command copy exited with code 4 when building - Visual Studio restart solves it

visual-studio

提问by Martin S Ek

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.

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

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.
  • 有时:重新启动 Visual Studio,我就可以构建解决方案
  • 有时:重新启动 Visual Studio 和我选择的文件管理器 (Q-Dir 4.37) 都可以解决这个问题。

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?

仅供参考,我两周前卸载了 ReSharper 5.1.1,从那时起 Visual Studio 一直给我一些错误(其中包括无法调试)。我重新安装了 Visual Studio,从那以后它运行得更好,但仍然遇到这个问题。这可能与某些 ReSharper 的东西在某处有关吗?

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

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

采纳答案by Preet Sangha

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.

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

Another solution which only works on 32 bit is to use the unlockertool to release the windows handles on the file before the copy.

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

Edit: I've just realised that it works under 64 bits too.

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

回答by Vemul

While /Cmay 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.

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

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.

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

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

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

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

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.

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

回答by ValidfroM

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.

VS 试图复制一个不存在的文件的原因是因为 Post-build 事件命令。

After I cleared that, problem solved.

我清除后,问题解决了。

UPDATE:

更新:

As @rhughes commented:

正如@rhughes 评论的那样:

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

在此处输入图片说明

回答by ElGauchooo

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.

就我而言,拖尾\正在使 xcopy 崩溃(正如我使用的那样$(TargetDir))。就我而言$(SolutionDir)..\bin。如果您使用任何其他输出,则需要对此进行调整。

Also note that start xcopydoes 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!

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

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.

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

回答by akka16

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.

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

回答by Ganesh Patil

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.

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

回答by Satyen

Run VS in Administrator mode and it should work fine.

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

回答by Srihari Chinna

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.
  1. 当完整的字符串路径超过 254 个字符时。
  2. 当要复制的文件名错误时。
  3. 当目标路径错误时。
  4. 在复制的文件或目标文件夹上设置 readonly 属性时。

回答by Tangodancer

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.

我收到此错误是因为运行 TFS 构建服务的用户帐户没有写入目标文件夹的权限。Right-click on the folder-->Properties-->Security.

回答by Aditya Reddy

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.

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