12

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?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
leon22
  • 5,280
  • 19
  • 62
  • 100
  • 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? – Lasse V. Karlsen Jul 03 '19 at 11:42
  • @LasseVågsætherKarlsen No. This problem only occur when an unhandled exception is thrown, otherwise I can build normally! (see my question) – leon22 Jul 04 '19 at 05:30

10 Answers10

22

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

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)

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
leon22
  • 5,280
  • 19
  • 62
  • 100
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.

  • 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)](https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer) – Ram Fattah Jan 25 '22 at 22:30
  • Glad you found it useful! – Aribo Oluwatoba Jan 27 '22 at 04:08
  • Same. In Task Manager I had to kill the process "NET Host" and then I could delete the obj/bin folders. – Christopher Aug 05 '22 at 14:28
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.

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.

Erik Thysell
  • 1,160
  • 1
  • 14
  • 31
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.

PolicyWatcher
  • 447
  • 2
  • 5
  • 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](/help/privileges/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](/tour). – Paul Roub Apr 22 '21 at 14:35
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.

TStewartFan
  • 17
  • 1
  • 6
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.

Umang
  • 1
  • 1
-1

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

-2

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.

Metcalfe
  • 61
  • 6