在VS2012 c#项目的构建过程中,我一直得到这个错误

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.    

现在我知道该终止进程了

Weingartner.WeinCad.vhost.exe

(有时)有用,但这让我很紧张。有办法阻止这一切发生吗?

调试器设置为


当前回答

下面是一个脚本,绝对可以摆脱这个问题:

REM   This script is invoked before compiling an assembly, and if the target file exist, it moves it to a temporary location
REM   The file-move works even if the existing assembly file is currently locked-by/in-use-in any process.
REM   This way we can be sure that the compilation won't end up claiming the assembly cannot be erased!

echo PreBuildEvents 
echo  $(TargetPath) is %1
echo  $(TargetFileName) is %2 
echo  $(TargetDir) is %3   
echo  $(TargetName) is %4

set dir=C:\temp\LockedAssemblies

if not exist %dir% (mkdir %dir%)

REM   delete all assemblies moved not really locked by a process
del "%dir%\*" /q

REM   assembly file (.exe / .dll) - .pdb file and eventually .xml file (documentation) are concerned
REM   use %random% to let coexists several process that hold several versions of locked assemblies
if exist "%1"  move "%1" "%dir%\%2.locked.%random%"
if exist "%3%4.pdb" move "%3%4.pdb" "%dir%\%4.pdb.locked%random%"
if exist "%3%4.xml.locked" del "%dir%\%4.xml.locked%random%"

REM Code with Macros
REM   if exist "$(TargetPath)"  move "$(TargetPath)" "C:\temp\LockedAssemblies\$(TargetFileName).locked.%random%"
REM   if exist "$(TargetDir)$(TargetName).pdb" move "C:\temp\LockedAssemblies\$(TargetName).pdb" "$(TargetDir)$(TargetName).pdb.locked%random%"
REM   if exist "$(TargetDir)$(TargetName).xml.locked" del "C:\temp\LockedAssemblies\$(TargetName).xml.locked%random%"

REM PreBuildEvent code
REM   $(SolutionDir)\BuildProcess\PreBuildEvents.bat  "$(TargetPath)"  "$(TargetFileName)"  "$(TargetDir)"  "$(TargetName)"

REM References:
REM   http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
REM   http://stackoverflow.com/a/2738456/27194
REM   http://stackoverflow.com/a/35800302/27194

脚本需要从每个VS项目预构建事件中调用。

$(SolutionDir)\BuildProcess\PreBuildEvents.bat  "$(TargetPath)"  "$(TargetFileName)"  "$(TargetDir)"  "$(TargetName)"

其他回答

在任务管理器中检查任何运行.exe .exe的进程

我终于怎么修了。为什么我们不能在第一次调试后继续调试,因为第一次调试exe仍在运行。这样,在第一次调试之后,您需要进入任务管理器->进程选项卡->[您的项目名称exe]结束exe进程。

这对我很有用:)

看看另一个答案。基本上,你可以让MSBuild.exe进程在后台运行,消耗资源文件。如果你有任何构建前或构建后的任务,导致MSBuild通过命令行启动,尝试添加"/nr:false"标志到这个命令。但是,关于更具体的细节,请参见前面的回答。

引用:

一个解决方法是把它放在>项目的预构建事件命令行属性中(在构建事件选项卡中):

代码片段

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

if exist "$(TargetPath)" if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"

另一个拼凑,额,但它很容易,为我在VS 2013工作。单击项目。在属性面板中应该有一个名为Project File的条目,该条目带有一个值

(你的项目名).vbproj

更改项目名称—例如在末尾添加-01。被锁定的原始.zip文件仍然在那里,但不再被引用…这样你的工作才能继续。下次计算机重新启动时,锁将消失,您可以删除错误文件。