Recently I had an issue where when I would try to run and debug a web app inside Visual Studio 2017, I would get an error where the .dlls couldn’t be copied out to the bin directory, which meant I couldn’t actually run the app at all! I would keep getting this error:
“Could not copy “C:\MyProject\MyDLL.dll” to “C:\MyProject\bin\Debug\MyDLL.dll”
This error is usually caused by a lock on the target file, preventing it from being deleted.
There was only one problem – I had restarted Visual Studio, and it was still happening.
I even restarted my computer, which fixed the problem once, but subsequent runs brought the error back.
The Root Cause
The actual root cause ended up that I actually had multiple projects set up as my startup project, but ONE of the projects wasn’t set up to debug, while the other was.
When you set the configuration in Visual Studio for a project to run without debugging, it actually doesn’t give the app the hooks that Visual Studio needs to close the app once you select “Stop Debugging.” This causes you to have an orphaned process, particularly if the app is set up as a headless console app or a windows service, where you don’t see anything on your screen to give you an indication of what’s running that has a hold on the files in your project’s /bin directory.
The Solution
The solution is a few things:
- Open Task Manager and close any orphaned processes you may have spun up -or- restart your machine to ensure everything is cleaned out.
- Ensure that all the projects are set to a debug configuration. This is probably what you want, and it will fix the could not copy error permanently. If this is not what you want, leave it, but now you’re aware of the cause.
I hope this helps point you in the right direction, particularly on projects where you may have a web app and then a scheduler all in the same solution, but have a mix of debugger/no debugging for them and start to have the “Could not copy dll” error message.