title: Locking files when building in Visual Studio 2010
body: <p><strong>Hello there, Stackoverflow.</strong></p>
  <p>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:</p>
  <blockquote>
  <p>Unable to copy file &quot;obj\x86\Debug\TileEngine.exe&quot; to &quot;bin\x86\Debug\TileEngine.exe&quot;. The process cannot access the file 'bin\x86\Debug\TileEngine.exe' becuase it is being used by another process.</p>
  </blockquote>
  <p>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.</p>
  <p>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.</p>
  <p>I've tried different solutions both on this site as well as everywhere I can find on Google.</p>
  <h2>Some solutions I've found, but haven't worked for me</h2>
  <h3>Solution 1 - Using a pre-build script</h3>
  <p>In some different questions here on Stackoverflow, I've found one solution being that you go into <code>Project Properties &gt; Build Events</code> and then in the <em>Pre-build event command line</em> add:</p>
  <pre><code>if exist &quot;$(TargetPath).locked&quot; del &quot;$(TargetPath).locked&quot;
  if not exist &quot;$(TargetPath).locked&quot; move &quot;$(TargetPath)&quot; &quot;$(TargetPath).locked&quot;
  </code></pre>
  <p>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.</p>
  <p><em><strong>Note:</strong> 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 &quot;normal&quot; again, still with the same error though.</em></p>
  <h3>Solution 2 - Running Visual Studio as Administrator</h3>
  <p>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.</p>
  <h3>Solution 3 - Changing the AssemblyVersion</h3>
  <p>In this question, <a href="https://stackoverflow.com/questions/2895898/visual-studio-build-fails-unable-to-copy-exe-file-from-obj-debug-to-bin-debug">Visual Studio build fails: unable to copy exe-file from obj\debug to bin\debug</a>, I found another solution that included changing the <code>AssemblyVersion</code>, in the <code>Properties\AssemblyInfo.cs</code> file, to <code>&quot;2.0.0.0&quot;</code>.
  This, however, haven't made any difference whatsoever for me.</p>
  <h3>Solution 4 - Closing UserControl designers before building</h3>
  <p>According to some different answers here and there on the Internet, Visual Studio apparently uses the built project executable to render the <code>UserControl</code> designer(?). In my case, this is probably not it, though, since I use XNA mostly and it doesn't use the <code>UserControl</code> designer.</p>
  <h3>Solution 5 - Cleaning up resources when application quits</h3>
  <p>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 <code>Content</code> pipeline, therefore this solution wouldn't' make any real sense.</p>
  <p>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.</p>