32

Using Visual Studio 2010 beta, when I run my application within the IDE for debugging, it works perfectly the first time. However, after closing the debug session, either by closing the application or clicking the stop debugging button, all subsequent attempts to debug the application fail with:

Error 1 Unable to copy file "obj\Debug\Application.dll" to "bin\Debug\Application.dll". The process cannot access the file 'bin\Debug\Application.dll' because it is being used by another process.

Handle.exe from SysInternals does show handles open, but even if I close the handles, the error doesn't go away. Any attempts to delete the file manually result in an "Access Denied" error message.

To fix this, I have to completely restart Visual Studio, afterwhich the Debug session will work once and stop again.

I'm not entirely sure when this started happening, but I'm pretty sure it's fairly recently.

UPDATE: After I force close the handles on Application.dll, I get the following error from VS:

Error 1 Unable to copy file "obj\Debug\Application.dll" to "bin\Debug\Application.dll". The requested operation cannot be performed on a file with a user-mapped section open.

What the heck is a "user-mapped section"??

UPDATE 2: It appears that this problem occurs when I have a Form open in Design view when trying to debug. I'm going to do some more troubleshooting and then post my results.

UPDATE 3: I think I've narrowed it down to a form using a UserControl.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Chris Thompson
  • 16,203
  • 9
  • 45
  • 62
  • 1
    Have you tried killing the Applocation.vshost.exe process rather than restarting Visual Studio? – Nestor Nov 30 '09 at 06:11
  • Yes, it CANNOT be killed. The process will not die until I close VS. – Chris Thompson Nov 30 '09 at 06:45
  • 1
    "It appears that this problem occurs when I have a Form open in Design view" This did it for me. I was getting your error. When I closed all open XAML files, the error went away. – user2023861 May 31 '13 at 16:07
  • There is a very good answer to the same problem by Cody Gray here : http://stackoverflow.com/questions/11646047/error-cannot-access-file-bin-debug-because-it-is-being-used-by-another-proc – nbadaud Feb 03 '17 at 08:10

9 Answers9

14

To be honest with you, it sounds like a bug in VS2010. For some reason it isn't closing the open handles when the debugger stops. Killing the VS process automatically closes those handles, allowing you to access the file again. As a work around, you might look at unlocker it's free and works exceptionally well. I know that's not a great answer, but it should be faster than restarting VS. You might to consider sending a bug report too...

Unlocker doesn't work on 64-bit OS, LockHunter does though.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
9

Here is how I solved this problem

*I open the project Properties, *select the build tab, *Clear the output path, *and buid(this will create the dll in the root folder) *come back to the output path and select browse(browse to the bin directory to either debug/release)and voila!

Sean Kendle
  • 3,538
  • 1
  • 27
  • 34
samurab
  • 91
  • 1
  • 1
9

As per Error: Cannot access file bin/Debug/… because it is being used by another process answer by TarmoPikaro, sometimes Visual Studio creates multiple msbuild.exe ghost processes, which persist after build. These ghost processes seem to be causing file locks.

Solution 1 - Kill ghost MSBuild.exe's

Killing msbuild.exe's is a one time solution, it needs to be done per build basis.

You can kill the processes as follows mrtumnus:

taskkill /f /im MSBuild.exe

Solution 2 - Disable parallel builds in Visual Studio

You can disable parallel build once and for all:

Tools > Options > Projects and Solutions > Build and Run > "maximum numbers of parallel project builds" - by default it has value of 8, switch it to 1.

Of course builds are bit slower now, but mileage may vary depending on your use case.

This is related to Error: Cannot access file bin/Debug/... because it is being used by another process

Community
  • 1
  • 1
warren.sentient
  • 513
  • 6
  • 10
  • works well, no need to use 3rd party tools (like the ones suggested by the accepted answer) with this solution! – Emre Bener Feb 09 '23 at 07:28
3

I've seen the Windows Indexing Service cause this. Disabling it helped. Virus scanners can also be at fault. Mutliple Application.Close() calls can supposedly cause this, too.

Of course, since it always works the first time, I suppose these are unlikely.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Andy West
  • 12,302
  • 4
  • 34
  • 52
2

Had the same problem. The following things helped

  1. Closing all design files while debugging
  2. using unlocker

Also my application opens a port. While debugging an exception was thrown and program quit. While ending the program I closed the port. That helped too.

But definitely, bug with VS2010.

Drishti
  • 83
  • 7
1

I encountered the same problem and in my case I had the file in question open in Visual Studio. Closing all files helped.

Juha Palomäki
  • 26,385
  • 2
  • 38
  • 43
0

I faced the same error and I was stuck in it for many days. Finally resolved the issue. I was working on a project that had many class libraries added in it. I added the reference of these libraries to my main project and mistakenly added reference to same project to itself. So when I removed self reference, it worked.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Lali
  • 2,816
  • 4
  • 30
  • 47
0

I had this issue myself. I had the project properties window open and that apparently creates a file lock. Even after I closed the window the file lock remained and I had to restart VS.

P.S. I'm using VS 2019. Just posting this for anyone having the issue I had and coming to this post.

0

If you get this error on VS Code;

Click on terminal screen and use "Ctrl + C" for stop running.