File is locked by Visual Studio 2019

leon22 picture leon22 · Jul 3, 2019 · Viewed 15.6k times · Source

I have a weird problem in Visual Studio:

if an unhandled exception is thrown during the debugging process I'm not able to build my C# application:

Unable to copy file "C:\Projects\A\bin\A.dll" to "..\..\bin\A.dll". 
  The process cannot access the file '..\..\bin\A.dll' because it is being used 
  by another process.

Could not copy "C:\Projects\A\bin\A.dll" to "..\..\bin\A.dll". 
  Exceeded retry count of 10. Failed. 
  The file is locked by: "Microsoft Visual Studio 2019 (14904)"

Is there a permanent solution to solve this strange error? (my only workaround is currently to restart Visual Studio)

What I tried and not worked for me:

  • VS menu -> Tools -> Options -> Projects and Solution -> Build and Run -> and "set maximum number of parallel builds" to 1.
  • Stop AntiVirus
  • Enable Application experience service (not found in Windows 10)
  • untick VS menu -> Tools -> Options -> Projects and Solution -> General -> Allow parallel project initialization
  • untick Debug -> Options -> Debugging -> General -> Break all processes when one process breaks

It's also not practical to insert Pre-build event scripts in all of my projects! Link: https://blog.ndepend.com/visual-studio-script-saves-time-pain/

Does anyone have a solution to this problem?

Answer

Riaan Mastenbroek picture Riaan Mastenbroek · Oct 14, 2019

What worked for me - Close the Solution, Delete the bin and obj folder, re-open and build.