File is locked by Visual Studio 2019

36.1k Views Asked by At

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?

13

There are 13 best solutions below

3
leon22 On BEST ANSWER

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)

2
Riaan Mastenbroek On

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

3
Aribo Oluwatoba On

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.

0
Mohammad reza hedayat nia On

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.

0
Erik Thysell On

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.

1
PolicyWatcher On

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.

0
Metcalfe On

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.

1
Amrender singh On

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

0
TStewartFan On

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.

0
Umang On

I faced the same and it happened because the bash window that open after run, I did not close it, just minimized it, and it was still running. So when I again tried to run the program, it showed me that the file is locked by another process, and in my case it was obvious that I needed to exit the previously opened bash window.

Apart from trying the other work arounds, just check this one as well.

1
antonio On

Closing the Visual Studio 2022 not worked for me; no "Use Managed Compatibility Mode" option available in Debug/Options.

I'm using SysInternals handle <dllpath> and then TaskKill /PID <pid>.

This dll create a ProxyControl in destination process to Spy controls.[ManagedSpy]

enter image description here

0
Jaider On

I do not know if this is new to VS, but there is a .NET Host process, or several of them, and after I kill them, I was able to clean and build the solution. Hope this help!

enter image description here

0
Brad On

For SSIS packages debugging/running:

I was debugging in SSIS and this kept happening, if I restarted it would fix it most of the time (but very annoying). I could not delete the files as they were all still locked.

If you close VS and then go to task manager and find: "SSIS Debug Host (32 bit)" in task manager and reloaded VS it worked for me (yours may say (32 bit) may be different but the rest worked for me.