title: Visual Studio build fails: unable to copy exe-file from obj\debug to bin\debug
body: <p><em><strong>Update:</strong></em> <em>A sample project reproducing this bug can be found <a href="https://web.archive.org/web/20120124021751/https://connect.microsoft.com/VisualStudio/feedback/details/558848/vsip-rebuilding-a-project-with-open-designers-twice-causes-an-error-with-locked-bin-debug-dlls" rel="nofollow noreferrer">here at Microsoft Connect</a>. I have also tested and verified that the solution given in <a href="https://stackoverflow.com/questions/2895898/visual-studio-build-fails-unable-to-copy-exe-file-from-obj-debug-to-bin-debug/2994502#2994502">the accepted answer below</a> works on that sample project. If this solution doesn't work for you, you are probably having a different issue (which belongs in a separate question).</em></p>
  <hr>
  <p>This is a question asked before, both here on Stack Overflow and other places, but none of the suggestions I've found this far have helped me, so I just have to try asking a new question.</p>
  <p>Scenario: I have a simple Windows Forms application (C#, .NET 4.0, Visual Studio 2010). It has a couple of base forms that most other forms inherit from, it uses Entity Framework (and POCO classes) for database access. Nothing fancy, no multi-threading or anything.</p>
  <p>Problem: All was fine for a while. Then, all out of the blue, Visual Studio failed to build when I was about to launch the application. I got the warning <em>&quot;Unable to delete file '...bin\Debug\[ProjectName].exe'. Access to the path '...bin\Debug\[ProjectName].exe' is denied.&quot;</em> and the error <em>&quot;Unable to copy file 'obj\x86\Debug\[ProjectName].exe' to 'bin\Debug\[ProjectName].exe'. The process cannot access the file 'bin\Debug\[ProjectName].exe' because it is being used by another process.&quot;</em> (I get both the warning and the error when running Rebuild, but only the error when running Build - don't think that is relevant?)</p>
  <p>I understand perfectly fine what the warning and error message says: Visual Studio is obviously trying to overwrite the exe-file while it at the same time has a lock on it for some reason. However, this doesn't help me find a solution to the problem... The only thing I've found working is to shut down Visual Studio and start it again. Building and launching then work, until I make a change in some of the forms, then I have the same problem again and have to restart... Quite frustrating!</p>
  <p>As I mentioned above, this seems to be a known problem, so there are lots of suggested solutions. I'll just list what I've already tried here, so people know what to skip:</p>
  <ul>
  <li><p>Creating a new clean solution and just copy the files from the old solution.</p>
  </li>
  <li><p>Adding the following to the following to the project's pre-build event:</p>
  <pre><code> if exist &quot;$(TargetPath).locked&quot; del &quot;$(TargetPath).locked&quot;
      if not exist &quot;$(TargetPath).locked&quot; if exist &quot;$(TargetPath)&quot; move &quot;$(TargetPath)&quot; &quot;$(TargetPath).locked&quot;
  </code></pre>
  </li>
  <li><p>Adding the following to the project properties (.csproj file):</p>
  <pre><code> &lt;GenerateResourceNeverLockTypeAssemblies&gt;true&lt;/GenerateResourceNeverLockTypeAssemblies&gt;
  </code></pre>
  </li>
  </ul>
  <p>However, none of them worked for me, so you can probably see why I'm starting to get a bit frustrated. I don't know where else to look, so I hope somebody has something to give me! Is this a bug in VS, and if so is there a patch? Or has I done something wrong, do I have a circular reference or similar, and if so how could I find out?</p>
  <p>Any suggestions are highly appreciated :)</p>
  <p><strong>Update:</strong> As mentioned in the comment below, I've also checked using Process Explorer that it actually <em>is</em> Visual Studio that is locking the file.</p>