Visual Studio build fails: unable to copy exe-file

2019-01-04 05:00发布

Update: A sample project reproducing this bug can be found here at Microsoft Connect. I have also tested and verified that the solution given in the accepted answer below 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).


This is a question asked before, both here on Stack Overflow and other places, but none of the suggestions I've found this far has helped me, so I just have to try asking a new question.

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.

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 "Unable to delete file '...bin\Debug\[ProjectName].exe'. Access to the path '...bin\Debug\[ProjectName].exe' is denied." and the error "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." (I get both the warning and the error when running Rebuild, but only the error when running Build - don't think that is relevant?)

I understand perfectly fine what the warning and error message says: Visual Studio is obviously trying to overwrite the exe-file while it 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 works, untill I make a change in some of the forms, then I have the same problem again and have to restart... Quite frustrating!

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:

  • Creating a new clean solution and just copy the files from the old solution.
  • Adding the following to the following to the project's pre-build event:

    if exist "$(TargetPath).locked" del "$(TargetPath).locked"
       if not exist "$(TargetPath).locked" if exist "$(TargetPath)" move "$(TargetPath)" "$(TargetPath).locked"
    
  • Adding the following to the project properties (.csproj file):

    <GenerateResourceNeverLockTypeAssemblies>true</GenerateResourceNeverLockTypeAssemblies>
    

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?

Any suggestions are highly appreciated :)

Update: As mentioned in comment below, I've also checked using Process Explorer that it actually is Visual Studio that is locking the file.

30条回答
萌系小妹纸
2楼-- · 2019-01-04 05:47

I had same problem. It said could not copy from bin\debug to obj.....

When i build web project i found my dll were all in bin folder and not in bin\debug. During publish vs was looking for files in bin\debug. So i opened web project file in editor and look for instances of bin\debug and i found all the dll were mentioned as bin\debug\mylibrary.dll. I removed all \debug from the path and published again. This time vs was able to find all the dll in bin folder and publish succeeded.

I have no idea how this path got changed in web project file.

I spent more than 5 hours debugging this and finally found solution on my own.

This is the right answer.

查看更多
趁早两清
3楼-- · 2019-01-04 05:48

This has been filed multiple times on Connect, Microsoft's community bug reporting site. FYI, I believe this bug has afflicted Visual Studio since 2003 and has been fixed after RTM each time. :( One of the references is as follows:

https://connect.microsoft.com/VisualStudio/feedback/details/568672/handles-to-project-dlls-are-not-released-when-compiling?wa=wsignin1.0

查看更多
唯我独甜
4楼-- · 2019-01-04 05:48

I tried several solutions that you provided, but occasionally I still receive this error. I am positive that my process is not running, and when i try to delete the executable file with internet explorer it is removed from the file list, but then I press F5 and voila, the file is back. It has not been deleted at all.

But if i delete the file through the TotalCommander, the exe file is actually deleted and I can successfully build the project.

I am using windows 7 x64 and total commander 7.56a 32 bit.

查看更多
Root(大扎)
5楼-- · 2019-01-04 05:48

My solution has nothing to do with versions, processes being locked, restarting, or deleting files.

The problem was actually due to the build failing, and not giving the correct error. The actual problem was a design flaw:

// Either this should be declared outside the function, or..
SomeObject a = new SomeObject(); 

Task.Factory.StartNew(() =>
{
   while (true)
   {
      a.waitForSomething();
   }
});

// ...this should not be called
a.doSomething(); 

After changing the scope of "a" to outside the function, or not using "a" after Task.Factory.StartNew();, I was able to build again.

This happened when using VS2012 Update 4 on Windows7x64 sp1.

Error message:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3390,5): error MSB3030: Could not copy the file "obj\x86\Debug\xxx.exe" because it was not found.

查看更多
小情绪 Triste *
6楼-- · 2019-01-04 05:50

Disable antivirus and try. I was also facing that problem... but in my case antivirus was blocking my application when I disabled antivirus it resolved.

查看更多
放我归山
7楼-- · 2019-01-04 05:50

I'd suggest download Process Explorer to find out exactly what process is locking the file. It can be found at:

http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

查看更多
登录 后发表回答