File is locked by Visual Studio 2019
Asked Answered
S

15

13

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?

Subplot answered 3/7, 2019 at 9:27 Comment(2)
The file is locked by Visual Studio, are you creating analyzers, components, anything that would make Visual Studio load your assembly, as opposed to just loading the source code?Irisirisa
@LasseVågsætherKarlsen No. This problem only occur when an unhandled exception is thrown, otherwise I can build normally! (see my question)Subplot
S
6

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)

Subplot answered 22/7, 2019 at 7:10 Comment(3)
I think there must be a different root cause, as it doesn't really make sense that that option causes the effect.Rugby
@Rugby I also think so, but in the meanwhile I can live with it!Subplot
Enabling "Just my code" did the trick of solving your remark in my case.Falster
R
23

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

Roderich answered 14/10, 2019 at 15:5 Comment(2)
I find that just restarting VS is sufficient.Blab
Thanks, deleting bin and obj folder worked for me.Saiff
S
6

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)

Subplot answered 22/7, 2019 at 7:10 Comment(3)
I think there must be a different root cause, as it doesn't really make sense that that option causes the effect.Rugby
@Rugby I also think so, but in the meanwhile I can live with it!Subplot
Enabling "Just my code" did the trick of solving your remark in my case.Falster
M
2

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.

Macrobiotic answered 24/7, 2020 at 7:50 Comment(3)
this solution worked for me. Thank you for introducing me to Microsoft's Process Explorer software. I dowanloaded here Download Process Explorer (2.5 MB)Officeholder
Glad you found it useful!Macrobiotic
Same. In Task Manager I had to kill the process "NET Host" and then I could delete the obj/bin folders.Insulting
M
2

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

Manda answered 22/9, 2023 at 10:28 Comment(1)
If you can, its better to post that as text rather than as an image. meta.https://mcmap.net/q/12237/-windows-cmd-pipe-not-unicode-even-with-u-switchBlab
M
1

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

Melisamelisande answered 29/11, 2023 at 14:46 Comment(0)
P
0

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.

Planar answered 7/11, 2020 at 8:16 Comment(0)
P
0

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.

Preordain answered 4/1, 2021 at 9:42 Comment(0)
D
0

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.

Digit answered 22/4, 2021 at 14:26 Comment(1)
This post doesn't look like an attempt to answer this question. Every post here is expected to be an explicit attempt to answer this question; if you have a critique or need a clarification of the question or another answer, you can post a comment (like this one) directly below it. Please remove this answer and create either a comment or a new question. See: Ask questions, get answers, no distractions.Bundelkhand
A
0

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.

Aquino answered 24/5, 2022 at 18:33 Comment(0)
G
0

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.

Gaskill answered 10/3, 2023 at 9:49 Comment(0)
R
0

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.

Recur answered 31/1 at 14:54 Comment(0)
P
0

For ASP.NET projects running on IIS (not IIS Express), recycling the app pool connected with the web application before (or even during) the build usually unlocks the DLLs and allows the build to complete.

Petitioner answered 10/7 at 17:49 Comment(0)
G
-1

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.

Gracia answered 7/6, 2021 at 18:34 Comment(0)
G
-1

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

Gladi answered 19/10, 2021 at 22:38 Comment(1)
Clean build is not the solution.Tote
P
-1

I searched for the name of the task using the folder in the task manager and ended the task, this helped me run the project with no problems.

Pigsty answered 27/7 at 21:28 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewMicrometer

© 2022 - 2024 — McMap. All rights reserved.