Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Could not write to output file '...\obj\Debug\Foo.Bar.dll"

I've got this error while compiling a big c# solution in Visual Studio 2010. Each time after compilation I had to delete obj folder of one the projects used by this solution. Otherwise I got the following error:

Could not write to output file '...\obj\Debug\Foo.Bar.dll'
The process cannot access the file because it is being used by another process

I've was looking for a solution all over the Internet and actually found/tried few of them.

For example : a lot of people on dev forums were suggesting not to start the compilation while UserControl (in some other sources From) designer is opened.

Some other people used pre-build scripts for obj folder removal, this particular solution is acceptable, but if the issued project is a widely used library its recompilation will cause recompilation of "parent" projects.

like image 638
Salaros Avatar asked Sep 06 '12 13:09

Salaros


2 Answers

Finally I've discovered a solution for this VS2010 (SP1 too) "bug" and I want to share it with stackoverflow users.

In my case the problem was that csproj file was SELF-REFERENCING the locked '...\obj\Debug\Foo.Bar.dll' file. Crazy I know. So I solved this annoying issue by removing the following line from .csproj file:

<Reference Include="Foo.Bar">
  <HintPath>obj\Debug\Foo.Bar.dll</HintPath>
</Reference>
like image 131
Salaros Avatar answered Oct 08 '22 16:10

Salaros


I initially found another solution to the problem as VS seems to lock the assembly in the obj\debug folder. I added a pre-build script to the project which fixed my problem.

del "$(ProjectDir)obj\*.*" /S /F /Q

After seeing the answer given by Salaros, that was indeed the problem. I created a new usercontrol that uses a Server control from another project. For some reason VS sometimes creates a self-references to itself when you view the usercontrol in design mode. Even removing the self-reference fixes it until VS thinks its time to add the reference again. Haven't found an exact pattern for that part.

PS: I'm using vs2012

like image 23
Marcel Avatar answered Oct 08 '22 17:10

Marcel