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

其他回答

打开项目属性[菜单>项目>属性] 选择“调试”选项卡 取消勾选“启用visual studio托管进程” 启动调试[F5] 你会收到安全警告,只是“ok”。让应用程序运行 停止调试。 检查选项“启用visual studio托管进程”,在调试选项卡下, 现在,尝试开始调试,您将不会再次看到错误

[为我工作]

在我的情况下,它是Resharper单元测试运行器(加上NUnit测试,从来没有这样的问题与MsTests)。在杀死进程后,可以重建进程,而无需重新启动OS或VS2013。

其他测试运行程序,比如xUnit,也会导致同样的问题。

有帮助的方法是检查是否可以添加Dispose模式,例如,如果您正在添加DbFixture,而数据库联系人没有正确地处理。这将导致即使测试完成,程序集文件也被锁定。

请注意,您可以将IDisposable接口添加到DbFixture中,并让IntelliSense添加Dispose模式。然后,处理相关的包含属性,并显式地将它们赋值为null。

这将有助于以一种干净的方式结束测试,并在测试结束后立即解锁相关的锁定文件。

示例(DBFixture由xUnit测试使用):

public class DbFixture: IDisposable
{
    private bool disposedValue;
    public ServiceProvider ServiceProvider { get; private set; }
    
    public DbFixture()
    {
        // initializes ServiceProvider
    }
    
    
    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                // dispose managed state (managed objects)
                ServiceProvider.Dispose();
                ServiceProvider = null;
            }

            // TODO: free unmanaged resources (unmanaged objects) and override finalizer
            // TODO: set large fields to null
            disposedValue = true;
        }
    }

    // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
    // ~DbFixture()
    // {
    //     // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
    //     Dispose(disposing: false);
    // }

    public void Dispose()
    {
        // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }
}

测试类本身也需要相同的模式——它需要自己的Dispose方法(如上面的DbFixture类所示):

   public SQL_Tests(ITestOutputHelper output)
    {
        this.Output = output;
        var fixture = new DbFixture(); // NOTE: MS Dependency injection framework didn't initialize when the fixture was a constructor param, hence it is here
        _serviceProvider = fixture.ServiceProvider;
    } // method

因此,它需要在自己的dispose方法中处置其本地属性_serviceProvider,因为测试类构造函数SQL_Tests实例化了它。

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

@Geoff (https://stackoverflow.com/a/25251766/3739540)的回答很好,但在重新编译时抛出错误代码1。

下面是对我有用的(2>nul 1>nul在结束+退出0):

(if exist "$(TargetDir)*old.pdb" del "$(TargetDir)*old.pdb") & (if exist "$(TargetDir)*.pdb" ren "$(TargetDir)*.pdb" *.old.pdb) 2>nul 1>nul
(if exist "$(TargetDir)*old.dll" del "$(TargetDir)*old.dll") & (if exist "$(TargetDir)*.dll" ren "$(TargetDir)*.dll" *.old.dll) 2>nul 1>nul
exit 0

删除Bin文件夹中的Debug或Release文件夹下的所有.cache文件。