Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File is locked by Visual Studio 2019

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?

like image 294
leon22 Avatar asked Jul 03 '19 09:07

leon22


People also ask

How to remove file lock in Visual Studio?

While working in the server workspace, navigate to the View menu and choose Other Windows, then choose Source Control Explorer. In Source Control Explorer, open the shortcut menu for the folder or file from which you want to remove a lock, and then choose Unlock.

Why does my file say locked?

The file might be locked because: The file is shared and another user is currently editing it. An instance of the Office app is running in the background with the file already opened. The file has been marked as Final and can no longer be updated.

Is file locked C#?

Lock a file in C# using File. Lock() method . The lock method let you lock a file so another process cannot access the file. This works even if it has read/write access to the file.

What is use managed compatibility mode?

Use Managed Compatibility Mode: Replaces the default debugging engine with a legacy version to enable these scenarios: You are using a . NET language other than C#, Visual Basic, or F# that provides its own Expression Evaluator (this includes C++/CLI).


1 Answers

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

like image 105
Riaan Mastenbroek Avatar answered Sep 20 '22 17:09

Riaan Mastenbroek