在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

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

调试器设置为


当前回答

第一个消息错误解决方案:

在我的例子中,问题在于路径长度。

解决方案是减少/更改问题文件路径。如果问题出在项目中的文件上,你应该减少/更改存储库路径。

C:\Users\userTest\folder1\folder2\folder3\...\folderX\myProject\...\file.exe

如。

C:\Users\userTest\folder1\myProject\...\file.exe

换句话说,默认情况下路径长度不能超过260个字符。

原线程答案 (https://stackoverflow.com/a/73686473/12678101)

其他回答

在我的情况下,它是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实例化了它。

似乎通过更改项目的程序集名称就可以解决这个问题。

所以不是这样

我把它变成这个

注意,我只是把它从Increment and Recall改成了Increment_Recall,我只是去掉了空格。它现在对我来说工作得很好。

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

这对我很有用:)

这是因为你已经关闭了你的应用程序,但它仍然在后台运行。

临时解决方案:

进入任务管理器(Ctrl + Shift + Esc)。 进入进程选项卡,找到“YourProjectName.exe”。 如果找不到自己的进程,请勾选“显示来自所有用户的进程”。 结束处理

永久解决方案:必须通过编码关闭应用程序。这是代码…

System.Windows.Forms.Application.Exit();

您必须以所有形式将此代码放入表单的关闭事件中。例子:

private void frm_menu_FormClosing(object sender, FormClosingEventArgs e)
{
    System.Windows.Forms.Application.Exit();
}

如果您正在调试T4模板,那么这种情况经常发生。我的解决方案(在MS修复这个问题之前)将只是杀死这个进程:

任务管理器—> User—> T4VSHostProcess.exe

此过程仅在调试T4模板时出现,而不会在运行T4模板时出现。