Visual Studio might throw the error “Unable to copy file ‘objx86Debugfile.exe’ to ‘binDebugfile.exe’. The process cannot access the file ‘binDebugfile.exe’ because it is being used by another process” to you one fine day and leave you wondering how to get rid of it. It is actually Visual Studio itself that is keeping the file open, […]

This article was posted by Independent Software, a website and database application development company based in Maputo, Mozambique. Our website offers regular write-ups on technical and design issues, ranging from details at code level to 3D Studio Max rendering. Read more about Independent Software's philosophy, or get in touch with Independent Software.

Visual Studio might throw the error “Unable to copy file ‘objx86Debugfile.exe’ to ‘binDebugfile.exe’. The process cannot access the file ‘binDebugfile.exe’ because it is being used by another process” to you one fine day and leave you wondering how to get rid of it.

It is actually Visual Studio itself that is keeping the file open, and a subsequent project build cannot overwrite it. Also, there is seemingly no cause. The error just happens, and then refuses to go away until you restart Visual Studio, which will cause the lock on the file to go away.

The internet is full of fixes for this error, typically involving pre-build scripts that try to delete the locked .exe file so that the build process can finish, or using the task manager (or ProcessExplorer) to kill the Visual Studio debug hosting process. However, this is just fighting symptoms. There should be an actual point where this error starts to happen, and we should undo whatever we did then to make the error stop occurring.

In my experience, there is in fact a cause. For me, the error started to occur after I had made a small change to my project’s AssemblyInfo.cs:

This is because I wanted automatic version numbering. The original AssemblyInfo.cs will contain this line:

Now if you replace the last two digits of the assembly version with an asterisk, then Visual Studio will replace it with new values each time you build. The values it uses are actually based on the current date and time. This seems useful, since now you won’t be stuck with version 1.0.0.0 and Visual Studio increments versions each time you build, so you don’t have to do this manually.

However: Danger, Will Robinson, Danger! It seems to me that this automatic version numbering is actually the culprit behind the “cannot access the file” error. Apparently Visual Studio will keep track of generated executables, perhaps in an effort to increase the version number when you build a new one. Get rid of it and your file access problem goes away. In this instance, you’re really better off updating the version numbers yourself, or adding a plugin to Visual Studio that does it for you.

Comments

8 8 Responses to “Visual Studio: Unable to copy file ‘objx86Debugfile.exe’ to ‘binDebugfile.exe’. The process cannot access the file ‘binDebugfile.exe’ because it is being used by another process.”
  1. Peter says:

    we use versioning with the form 1.0.2.0 instead of 1.0.2.* and we still have the same versioning problem

  2. Felipe says:

    In my case was Avast! Free Antivirus.

    Usually the computer where i work, the antivirus is deactivated (because i just use it for programming), but i reactivated to analize a file and forgot about it, then i added some images to the project (as Reference) and when i tried to rebuild the solution, this error showed up.

    It took me a lot to “fix” this problem because i completly forgot about the antivirus… So… you may want to add this “solution” to your check list.

    Saludos ^^

  3. Ish says:

    Your Post is perfect thanks

  4. Dave says:

    Nope, not antivirus, not versioning. It’s a bug in VisualStudio (that STILL has not been fixed).

  5. George says:

    I solved this by going to the properties of the project > tab “Debug” > uncheck “Enable the Visual Studio hosting process”. But I don’t know if there are other unknown implications.

    http://stackoverflow.com/questions/5134137/build-error-the-process-cannot-access-the-file-because-it-is-being-used-by-ano

    • beosma says:

      Thank you sir for finding this! I never had this problem occur until about 2 months ago and I have been using vb2010 express for years. I have tried multiple workarounds and nothing worked or lasted more than one time. I unchecked the hosting checkbox and the next time I started, I got a message about security when building for release. Other than that no problems or side effects. I just check the box for security when getting ready to publish for release. Again thank you for this, I have stressed over this with several projects now and the upgrade of some as well. -Beo

  6. wiratmojo says:

    that seems really annoying issue i agree with it… i have faced this issue the solution is go to

    Right Click My Project (it will always below your VS project name at solution explorer) – Tab Security – Uncheck Enable Click Once Security Setting

    done,
    that will close your trouble

    • nethead717 says:

      Unchecking the “Enable ClickOnce Security Settings” box did indeed fix this for me too. Thanks wiratmojo!

Leave a Reply

Your email address will not be published. Required fields are marked *