File is locked by Visual Studio 2019

Viewed 21031

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?

9 Answers

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

It's a little bit weird but the problem was caused by the Debug option Use Managed Compatibility Mode! So just untick Debug -> Options -> Use Managed Compatibility Mode and no more files locked errors by Visual Studio!

Remark: The downside of disabling this option is you will get annoying long time consuming evaluation errors by VS! (More information)

Deleting the obj and bin folders were impossible for me, so i had to install Microsoft's Process Explorer to find the application's dll process still running. Killing this process worked for me.

it is a little stupid but: In windows task manager in Process tab, just find Microsoft visual studio <2019>, click "^" to collapse it's child's tree and find "Microsoft Visual Studio XAML Designer" select "Microsoft Visual Studio XAML Designer" and simply end process it before every debug.

Deleting was impossible for me but it turned out that VS2019 kept a debug version of the software running that didn't close automatically when I stopped debugging. In Task manager -> close the running app and I could start the build/debug again.

I have encountered this in a WinForms project in VS2019, where none of the other suggested solutions has worked, and where exiting VS and attempting to delete the affected files was refused by the O/S still claiming the files were open.

The fix that has worked here in this case was to go to Task Manager, and to find and terminate a copy of VBCSCompiler that is being left running even after VS has terminated. It shows up under "Background Processes".

After that, starting VS again worked fine.

Run Clean before Building or use Rebuild option. VS will build it fine. Bit slow , but it works everytime. No VS restart required.

Deleting worked for me as well but only after I force exited the DTSDebugHost from taskmgr for any others that couldn't get it to work.

Simple fix in my case. I already had my application running when I tried to run it again and got this error. Closing it solved my problem.

Related