Visual Studio “Could not copy” … during build

2019-01-03 20:53发布

I keep getting this error during the build of my VS2012 C# project

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.    

Now I've figured out that killing the process

Weingartner.WeinCad.vhost.exe

works (sometimes ) but this is getting on my nerves. Any way to stop this happening at all?

My debugger settings are

enter image description here enter image description here

30条回答
倾城 Initia
2楼-- · 2019-01-03 21:20

@Geoff's (https://stackoverflow.com/a/25251766/3739540) answer is good, but it throws error code 1 on recompile.

Here is what worked for me (2>nul 1>nul on the end + exit 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
查看更多
ら.Afraid
3楼-- · 2019-01-03 21:20

I have noticed some answers that solved my problem, BUT, just in case anyone is having the same problem I was.

IF YOU ARE RUNNING A CONSOLE APP: BEFORE YOU DO ANYTHING ELSE.

Make sure you have closed out any console windows that may have been opened from a previous build. For instance I was just testing some code in a console application, I didn't realize that the console window from one of the previous times I ran my program was open. During that session I was debugging, the window got pushed to the back and I couldn't see it. Just saying, this could be your problem, so check to make sure that is not the problem.

查看更多
做个烂人
4楼-- · 2019-01-03 21:22

For me it was the Avast antivirus that wont let visual studio to write/read/execute file. So I had to add Visual studio 2010/2012 folder to antivirus exclusion list. And right after that baam... it works.

查看更多
叛逆
5楼-- · 2019-01-03 21:23

Exception

In some cases in Visual Studio when you (Build || Rebuild) on top of running IISExpress you faced with this Exception:

Unable to copy file "obj\Debug\YourProjectName.dll" to bin\YourProjectName.dll". the process cannot access the file 'bin\YourProjectName.dll' because it is being used by another process

Solution

  1. Right click on web project that needs to build.
  2. Click on properties.
  3. Select Build Events Tab on the left side.
  4. In Pre-build events command line paste these 2 line:
tasklist /fi "imagename eq iisexpress.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "iisexpress.exe"

You are good 2 GO!

查看更多
我命由我不由天
6楼-- · 2019-01-03 21:25

I finally how fix it. Why we can't continue debug after the first debug because the first debug exe still running. So that, after first debug, you need to go to Task Manager -> Process Tab -> [your project name exe] end the exe process.

it works for me :)

查看更多
太酷不给撩
7楼-- · 2019-01-03 21:26

the .vhost.exe is a debugger process, so it appears that the process being debugged hasn't closed properly. Chances are you have a bug that's keeping it alive and are not stopping the debug process correctly - there are options to detach from the process when you click 'stop debugging' instead of actually killing the debugger so maybe you have that set.

But that's the problem - the file you're trying to copy over is locked (ie still being used) by the OS so its preventing the copy. Ensure that file is free and you'll be able to copy.

查看更多
登录 后发表回答