Visual Studio “Could not copy” … during build

2019-01-03 20:53发布

I keep getting this error during the build of my VS2012 C# project

Error   41  Could not copy "obj\Debug\WeinGartner.WeinCad.exe" to
 "bin\Debug\WeinGartner.WeinCad.exe". 
 Exceeded retry count of 10. Failed.    


Error   42  Unable to copy file "obj\Debug\WeinGartner.WeinCad.exe" to
"bin\Debug\WeinGartner.WeinCad.exe". The process cannot access the file
'bin\Debug\WeinGartner.WeinCad.exe' because it is being used by another 
process.    

Now I've figured out that killing the process

Weingartner.WeinCad.vhost.exe

works (sometimes ) but this is getting on my nerves. Any way to stop this happening at all?

My debugger settings are

enter image description here enter image description here

30条回答
倾城 Initia
2楼-- · 2019-01-03 21:15

Quote:

A workaround is to put this in the Pre-build event command line property of the >project (In the build Events tab):

Code Snippet

if exist "$(TargetPath).locked" del "$(TargetPath).locked"

if exist "$(TargetPath)" if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
查看更多
在下西门庆
3楼-- · 2019-01-03 21:15

Killing the process w3wp.exe (IIS) will often solve this.
Generally, you can know the process that has the lock on the file by navigating to the bin folder and trying to delete it. The error message that will pop up, in case another process is using it, will contain the name of the process that needs to be killed.

查看更多
干净又极端
4楼-- · 2019-01-03 21:15

Add in pre-build event of your master project taskkill /f /fi "pid gt 0" /im "YourProcess.vshost.exe"

查看更多
地球回转人心会变
5楼-- · 2019-01-03 21:17

It seems that by change the assembly name of a project fixes the problem.

So instead of this

enter image description here

I change it to this

enter image description here

Notice that I just changed it from Increment and Recall to Increment_Recall, I just removed the spaces. It is now working fine to me.

查看更多
唯我独甜
6楼-- · 2019-01-03 21:18

I have encountered similar error messages in Visual Studio 2013.

Mostly, I have found that this situation has occurred when a debug process was halted because of an exception.

When clean+build has not resolved this problem for me, I have had success by doing the following:

  • Closing Visual Studio
  • Deleting the bin and obj folders, and
  • Reopening Visual Studio.

This "bug" has existed since Visual Studio 2003.

Finally, I have also found that I can often overcome this problem by simply renaming the executable file and then deleting it.

查看更多
放我归山
7楼-- · 2019-01-03 21:19

Killing the vstest.executionengine.exe process(es) resolves this issue 90% of the time for me. If that doesn't work, then also killing QTAgent32.exe and then deleting the /bin and /obj folders for the project in question works.

This is the most irritating part of my work day. :)

查看更多
登录 后发表回答