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

其他回答

我不能给出一个解决方案来防止这种情况发生,但你至少可以重命名锁定文件(windows资源管理器,或经典命令窗口),然后编译/构建。不需要重启VS201x。如果有一些经验,你可以添加一个预构建脚本来删除旧文件或重命名,以防出现锁。

我在Windows 8上的VS 2012 Version 11.0.60610.01 Update 3上遇到了同样的问题

没有打开设计器窗口,项目是一个简单的控制台应用程序。

删除访问该文件的vshost进程在大多数情况下都不起作用,因为该进程没有访问该文件。

最简单的工作方法是将项目从解决方案中删除,在解决方案中构建另一个项目,然后将原始项目添加回来。

这是一种刺激和浪费时间,但这是我所知道的所有其他选择中最便宜的。

希望这对你有所帮助……

我正在研究一个微服务项目解决方案,我需要同时运行两个项目,冲突发生在lunchsettings.json applicationurl: {portNo}部分用于两个正在运行的项目是相同的端口

lunchsettings。第一个项目的Json

...
"Project#1": {
  ...
  "applicationUrl": "http://localhost:5001",
  ...
}

lunchsettings。第二个项目的Json

...
"Project#2": {
  ...
  "applicationUrl": "http://localhost:5001",
  ...
}

来修复它

lunchsettings。第一个项目的Json

...
"Project#1": {
  ...
  "applicationUrl": "http://localhost:5001",
  ...
}

lunchsettings。Json for secondproject

...
"Project#2": {
  ...
  "applicationUrl": "http://localhost:5002",
  ...
}

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

临时解决方案:

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

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

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

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

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

我能够修复这个问题(VS 2010)通过提供以下预构建操作;

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

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