在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

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

调试器设置为


当前回答

我在VS 2015中遇到了这个问题。我的环境的原因是使用StyleCop项目设置StyleCopAdditionalAddinPaths Include="…"来指定额外的StyleCop Addin路径。我使用的变通方法是从.csproj文件中删除这个项目设置,取而代之的是手动复制StyleCop AddIn中的StyleCop. csharp . rules .dll。不是一个优雅的解决方案,但我发现解决方案从来没有锁定dll之后,这样做。

其他回答

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

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

我找到了一个完整的解决方案!

大多数答案告诉你杀死进程,然而与进程黑客,我找不到任何。

我找到了一个相对简单的解决方案。

在窗体设计器中选择您的主窗体。 单击属性菜单上的事件选项卡。 双击事件FormClosing。这将自动生成事件系统和函数:

private void[你的表单名]_FormClosing(对象发送器,FormClosingEventArgs e)

在这个函数中,添加Application。退出

像这样:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Application.Exit();
}

有用的图像

我希望这能有所帮助!这道题真糟糕!

修复2

打开一项名为“应用体验”的服务。

如果没有一个答案是正确的,试试这个简单的检查。 找到任何MSbuild.exe运行和持有您的项目EXE。杀死MSBuild.exe,你应该很好。

重置IIS,停止使用您的DLL的服务(可能是控制台应用程序或windows服务托管应用程序或IIS),然后尝试。

这对我很管用。

@Gerard的答案是正确的。

当clean+build不能为我解决这个问题时,我通过以下方法取得了成功:

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

但我需要在主机上做一些额外的工作:

> Add-Migration Initial
> Update-Database

然后我开始调试,它起作用了。