16

Hello there, Stackoverflow.

Recently, when I've been programming in Visual Studio 2010, I've been getting the problem with VS locking the bin/Debug/(ProjectName).exe file when trying to build and gives me the error below after trying to build the project 10 times:

Unable to copy file "obj\x86\Debug\TileEngine.exe" to "bin\x86\Debug\TileEngine.exe". The process cannot access the file 'bin\x86\Debug\TileEngine.exe' becuase it is being used by another process.

The problem appears when I edit the source and then try to Debug. I've checked using different programs, and the only program using the file is Visual Studio.

If I wait for about 10 minutes before trying to build, it seems to work properly, but when trying different things, it isn't good needing to wait 10 minutes before trying something.

I've tried different solutions both on this site as well as everywhere I can find on Google.

Some solutions I've found, but haven't worked for me

Solution 1 - Using a pre-build script

In some different questions here on Stackoverflow, I've found one solution being that you go into Project Properties > Build Events and then in the Pre-build event command line add:

if exist "$(TargetPath).locked" del "$(TargetPath).locked"
if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"

This made it possible for me to build the project one more time than I usually could, but when editing the code again, and then building, the same error appeared.

Note: Trying to build a release instead of a debug build seems to break the pre-build script and it exits with the code '1', which seems to make VS unable to build properly. Removing the pre-build script makes it work like "normal" again, still with the same error though.

Solution 2 - Running Visual Studio as Administrator

This is another solution I've found, but havent worked either for me, so I assume that Visual Studio already have all the permissions required and running as Administrator doesn't actually make any difference.

Solution 3 - Changing the AssemblyVersion

In this question, Visual Studio build fails: unable to copy exe-file from obj\debug to bin\debug, I found another solution that included changing the AssemblyVersion, in the Properties\AssemblyInfo.cs file, to "2.0.0.0". This, however, haven't made any difference whatsoever for me.

Solution 4 - Closing UserControl designers before building

According to some different answers here and there on the Internet, Visual Studio apparently uses the built project executable to render the UserControl designer(?). In my case, this is probably not it, though, since I use XNA mostly and it doesn't use the UserControl designer.

Solution 5 - Cleaning up resources when application quits

This might be a solution that I have failed to implement properly. I'm just thinking though, that if this is the solution, how come I haven't been required to do it before. I assume XNA unloads everything that gets loaded through the Content pipeline, therefore this solution wouldn't' make any real sense.

If there is anyone that is able to spread some light on this issue, it would be really awesome, as it is stopping me from programming anything really, because I don't like waiting for 10 minutes because I've made a 2 second change all the time.

Community
  • 1
  • 1
stripe103
  • 245
  • 3
  • 9

11 Answers11

2

I've run into this problem a few times myself.

Mine might not be from the same cause as yours, but I'll tell you what went wrong with me and how I fixed it, hopefully it'll be helpful to you.

Basically, my program never fully exited properly, even when it appeared to. It would continue to run, and thus continue to lock down the file.

A quick dirty fix I used initially (and a way to prove if this is the case) is:

  • Open Task Manager (Ctrl-Alt-Del)
  • Click Processes tab
  • Look for your program's name (TileEngine.exe)
  • Note: There will probably be name_vshost.exe (TileEngine_vshost.exe) That's a VisualStudio thing, ignore that, it's not relevant.
  • If you find it, it means your program hasn't actual exited fully.
  • If it's there, click on it and press "End Process"

So if it's there, then for some reason, your program didn't shut down, like mine did.

Often, this is from a thread being launched and forgotten, or an Async task that never completes, or something like that.

Make sure in your OnExiting(..) void function that you kill all running threads.

If your program is still running despite best attempts to close all threads and other blockers, you can use the very dirty bad method: In OnExiting(...) run the code "System.Diagnostics.Process.GetCurrentProcess().Kill();" - this will taskmanager-style forceshutdown the current process... this is only as an emergency I-can't-make-it-work-any-other-way method.

mcmonkey4eva
  • 1,359
  • 7
  • 19
  • Yeah, that was my first thought as well. But when looking in the task manager, or even more advanced programs such as Sysinternals Process Explorer, the process wasn't there. – stripe103 Jun 10 '13 at 09:43
2

I think I found the solution myself. In the Project Properties, "Enable the Visual Studio hosting process" wasn't checked. Checking it seems to have fixed the problems, at least for now.

Got reminded of it from mcmonkey4eva's post. So thanks for that =)

And thanks for the other replied I've got. Stackoverflow is awesome!

stripe103
  • 245
  • 3
  • 9
  • 1
    Hmm... well, technically it wasn't the answer, but reminded me of checking something that then turned out to be the answer. But sure =) – stripe103 Jun 10 '13 at 10:04
  • This is what solved it for me. A recent update to VS enabled this option in my project which wasn't previously there. I was about ready to smash the screen! – Brett Ryan Mar 18 '16 at 00:48
1

Have you checked if any files are being blocked by your firewall? When I switched to the full version of Avast I find I have to disable the File System Shield It loves to remove my executable files when I try to run my visual studio projects.

I had problems when upgrading to VS2012 Professional. (SDK, .Net, Visual C++ Redistributable package)

ENSURE ALL OF THESE ARE COMPATIBLE WITH THE CURRENT VERSION OF VS YOU ARE USING

What I did, was I ended up uninstalling EVERYTHING that was associated with both Visual Studio downloads. If you are able to remove and save your project files elsewhere and then bring them back. Go through all your program files to see if there is anything hidden in the wrong folder and check your C drive.

Which meant downloading and reinstalling (fresh):

I think if you clean out your program files, it should be ok. I wouldn't recommend going into your register unless you are very sure of what you are doing. IF you have already made changes to the register then we'll have a look at that and other options (if this doesn't solve your problem).

  • Yeah I am not going to try to edit the register. Although CCleaner might have done something wrong when "fixing" registry errors. Maybe? Turning off the file system shield didn't work though. Still the same error. I'll have a go at reinstalling Visual Studio later when I have time. – stripe103 Jun 09 '13 at 13:13
  • Yeah I kindof suspect CCleaner to be the bad guy here. It was trying to remove some of the filtypes used by for example Visual Studio, like the .suo, .cpp, .h, and .cs filetypes. I have tried using the "Change/Reinstall" option in the VS installer, and had it do whatever it really does, but nothing have changed and the problem is still there. I hope I don't need to reinstall Windows again. – stripe103 Jun 09 '13 at 15:12
  • I do, but it's very long time ago. I'm not sure what other changes I've done since. – stripe103 Jun 10 '13 at 09:38
  • Nah I didn't, I just used the installer in the VS Iso. But I seem to have found the solution to the problem anyway. At least a temporary solution. – stripe103 Jun 10 '13 at 09:50
1

Try removing the readonly check from your solution by unchecking at the folder level.

Arushi Agrawal
  • 619
  • 3
  • 10
1

I ran into this problem and in my case was due to having bin included in the solution; as soon as I excluded the bin folder from my solution the problem went away.

1

Nothing helped, not prebuild commands, neither designers closing, but I figured out a way that helped me, just changing from debug to release and vice-versa releases locked files and you can delete them without closing the IDE.

Kakha Middle Or
  • 166
  • 1
  • 6
0

I regularly get this problem if I switch from Debug to Release and then immediately F5 to compile. Crazy as it sounds, waiting for, say, one minute after switching between modes will prevent this.

If it's locked, the only solution is to close Visual Studio and re-open.

GlennG
  • 2,982
  • 2
  • 20
  • 25
0

I solved this problem organizing my resources on solution. I noticed this error when I put some images on my application at the same solution folder.

So,

  • I'd removed all images and resources from app, save without it.
  • Moved the images to outside Solution folder.
  • Open the solution and add this images again, using "Import" button at controls.

If you will try this, remember to do with Application Icon, on Project Settings.

Now, everything is working fine for me.

I hope it helps.

0

You need to disable Windows Indexer as it locks up the file

Follow this Guide how to disable

bluee
  • 997
  • 8
  • 18
0

In my case the problem seem to be caused by the remote debugger. It starts on local machine when you compile with "x64" option. Try to change the project setting (properties/buid) until you reach the final version.

0

Change Build Platform target from x86 to Any CPU.

Nic Nilov
  • 5,056
  • 2
  • 22
  • 37