Error: Cannot access file bin/Debug/… because it i

2019-01-03 22:39发布

When I debug my project, I get following error:

"Unable to copy file "obj\Debug\My Dream.exe" to "bin\Debug\My Dream.exe". The process cannot access the file 'bin\Debug\My Dream.exe' because it is being used by another process."

Using Process Explorer, I see that MyApplication.exe was out but System process still uses it although I stopped debug before. Whenever I change my code and start debug it is going to happen. If I copy project to USB and debug, it runs OK.

Why? How can I fix this error?

I use Window 7 Professional. With Xp I have never got this error.

26条回答
Deceive 欺骗
2楼-- · 2019-01-03 23:01

See my answer here if you're having this problem while running unit tests. Answer copied below:

Building upon Sébastien's answer, I added a pre-build step to my test project to automatically kill any vstest.* executables still running. The following pre-build command worked for me:

taskkill /f /im vstest.*
exit 0

The exit 0 command is at the end to prevent build failure when there are no vstest.* executables running.

查看更多
狗以群分
3楼-- · 2019-01-03 23:02

I understand this is an old question. Unfortunately I was facing the same issue with my .net core 2.0 application in visual studio 2017. So, I thought of sharing the solution which worked for me. Before this solution I had tried the below steps.

  1. Restarted visual studio
  2. Closed all the application
  3. Clean my solution and rebuild

None of the above steps didn't fix the issue.

And then I opened my Task Manager and selected dotnet process and then clicked End task button. Later I opened my Visual Studio and everything was working fine.

enter image description here

查看更多
迷人小祖宗
4楼-- · 2019-01-03 23:02

I have been plagued by this issue in Visual Studio 2017. It started about two or three weeks ago, and has severely eaten into my productivity. Clean and Rebulid haven't worked; even restarting my machine doesn't do the job.

One way to deal with the issue is to clean the offending assembly, and to build (as opposed to rebuild) the project you want to run immediately afterwards. This works about 30% of the time.

However, probably the most reliable solution I've found is to open a Developer Command Prompt, and use msbuild directly. I've been doing this for the last three days, and so far the problem hasn't happened once.

查看更多
祖国的老花朵
5楼-- · 2019-01-03 23:04

One simple solution is you go to bin\Debug folder, delete all the files in that folder, then rebuild. If it doesn't work, close Visual Studio then go to bin\Debug folder using file explorer, on the left coner, click on File> Open Command Prompt> Open Command Prompt as Administrator > Enter this command "DEL /F /Q /A *" > then rebuild

查看更多
我命由我不由天
6楼-- · 2019-01-03 23:05

i found Cody Gray 's answer partially helpful, in that it did direct me to the real source of my problem which some of you may also be experiencing: visual studio's test execution stays open by default and maintains a lock on the files.

To stop that predominantly useless behaviour, follow the instructions from https://connect.microsoft.com/VisualStudio/feedback/details/771994/vstest-executionengine-x86-exe-32-bit-not-closing-vs2012-11-0-50727-1-rtmrel

Uncheck Test menu -> Test Settings -> "Keep Test Execution Engine Running"

查看更多
手持菜刀,她持情操
7楼-- · 2019-01-03 23:07

I've had this error crop up on me before, even in Visual Studio 2008. It came back and more prevalent in Visual Studio 2012.

Here is what I do.

Paste this in the troublesome project's pre-build event:

if exist "$(TargetPath).locked" del "$(TargetPath).locked"
if exist "$(TargetPath)" if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
查看更多
登录 后发表回答