Microsoft KB Archive/313512

= BUG: &quot;Could not copy temporary files to the output directory&quot; error message when you build a solution that contains multiple projects =

Article ID: 313512

Article Last Modified on 1/26/2006

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft Visual Studio .NET 2002 Academic Edition

-



This article was previously published under Q313512



SYMPTOMS
When you build a solution that contains multiple projects in Visual Studio .NET, you may receive one of the following error messages:

Error message 1

Could not copy temporary files to the output directory.

The file ' ' cannot be copied to the run directory. The process cannot access the file because it is being used by another process.

Error message 2

error CS0016: Could not write to output file ' ' -- 'The process cannot access the file because it is being used by another process. '

Error message 3

Cannot delete the project output: is the file read-only? A failure occurred while attempting to start the application.

Error message 4

Make sure that the file is not open by another process and is not write-protected.

When you try to build a solution that contains an .exe project, and the solution also depends on a second Class Library project or DLL Assembly, you may receive a build error message that is similar to the following:

The type or namespace name 'XXXXX' could not be found. (Are you missing a using directive or an assembly reference?) Cannot copy assembly 'Tools' to file 'I:\samplesViews\Bin\Debug\XXXXX.dll'. The process cannot access the file because it is being used by another process.

The error message is generated on every other build of the solution.



CAUSE
This problem may occur when one of the assemblies that you compile is larger than 64 kilobytes (KB) and one (or both) of the following conditions is true:
 * Your solution contains projects that are compiled to the same output folder.
 * The Copy Local property on one of the referenced assemblies or projects is set to False.



RESOLUTION
To work around this problem, do any (or all) of the following, as appropriate for your projects:
 * Compile the outputs for individual projects to different folders. Visual Studio .NET 2002 and Visual Studio .NET 2003 do not support a common output folder.
 * Set the Copy Local property for the referenced assembly or project to True.
 * Verify that you do not have the Object Browser window open.
 * Verify that you do not have the same project (or projects) open in another instance of Visual Studio .NET.

Note Microsoft strongly recommends that you use project references instead of assembly references. Assembly references do not contribute to the file locking problem. However, when you use assembly references, you must set the build order manually. When you use project references, the build order is established automatically.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



MORE INFORMATION
This problem occurs when the IntelliSense engine of the project locks a referenced assembly. The IntelliSense engine locks only those referenced assemblies that are larger than 64 KB.

To resolve this assembly locking problem, the master copy of the referenced assembly must be left unlocked. You must provide another &quot;lockable&quot; copy of the referenced assembly to the IntelliSense engine. If the assembly reference has the Copy Local property set to True, and the project is being built to its own output directory, a lockable copy of the referenced assembly already exists.

The Copy Local property determines whether a private copy of a reference will be copied to the output path of the project. When a reference is added, the default value for this property is computed by the project system. Microsoft strongly recommends that you do not change this default value. Therefore, when you have the Copy Local property set to True for the referenced assembly, the IntelliSense engine locks this private copy of the assembly in the local Bin folder. This private copy of the assembly is automatically updated each time that the master copy of the assembly is rebuilt.

Steps to reproduce the behavior
This article describes two different scenarios to reproduce this problem:
 * Common output folder
 * Copy Local is set to FALSE

Both of these scenarios require an assembly that is larger than 64 KB. To create this assembly for the examples in this article, follow these steps.

Note Verify that the line numbers appear in your C# code editor. Step 8 is easier to do when the line numbers are visible.  Create a new folder named C:\Test . Start Visual Studio .NET. On the File menu, point to New, and then click Project. Under Visual C# Projects, click Class Library. In the Name text box, type MyLibrary. In the Location text box, type C:\Test . Click OK.</li> In Solution Explorer, click MyLibrary. On the Project menu, click Properties, and then click Build under Configuration Properties. Change the Output path setting to the C:\Test folder. Click OK to save the project and close the MyLibrary Property Pages dialog box.</li> In Solution Explorer, double-click Class1.cs to open the code window of the default class.</li>  Paste the following function in Class1: public void MyMethod {               Console.Write(&quot;.&quot;); Console.Write(&quot;.&quot;); Console.Write(&quot;.&quot;); Console.Write(&quot;.&quot;); // Repeat // Console.Write(&quot;.&quot;); // about 5,500 times. } </li> On the Build menu, click Build MyLibrary to compile and create the MyLibrary.dll file. Make sure that the output MyLibrary.dll file in C:\Test is larger than 64 KB.</li></ol>

Common output folder
<ol> Start Visual Studio .NET, and then load the MyLibrary class library project that you created in the previous section.</li> On the File menu, point to New, and then click Project.</li> Under Visual C# Projects, click Console Application.</li> In the Name text box, type ConApp. In the Location text box, type C:\Test .</li> Click Add to Solution.</li> Change the ConApp Output Path setting to C:\Test.</li> On the Project menu, click Add Reference. On the Projects tab, click Select. Click OK to add the reference to the MyLibrary project.</li> On the Project menu, click Set as StartUp Project to set ConApp as the startup project.</li> In Solution Explorer, double-click Class1.cs to open the code window of the default class for the ConApp project.</li>  Add the following code to the Main method of ConApp: MyLibrary.Class1 o = new MyLibrary.Class1; o.MyMethod; </li> On the Build menu, click Build ConApp.

The build succeeds.</li>  Open Class1 in the MyLibrary project, and then delete one of the following lines of code: Console.Write(&quot;.&quot;); Save Class1. </li> <li>Repeat step 11.

You receive one of the error messages that is described in the &quot;Symptoms&quot; section of this article.</li></ol>

Copy Local is set to FALSE
Note If you have tried the steps in the &quot;Common Output Folder&quot; scenario, Visual Studio .NET will lock MyLibrary.dll. To try the steps in the &quot;Copy Local Is Set to FALSE&quot; scenario, you must quit Visual Studio .NET, and then delete the C:\Test\MyLibrary\obj folder. <ol> <li>Create a new folder named C:\Test2 .</li> <li>Start a new instance of Visual Studio .NET, and then load the MyLibrary class library project. Verify that you do not have the ConApp project loaded in the solution.</li> <li>On the File menu, point to New, and then click Project. Under Visual C# Projects, click Console Application.</li> <li>In the Name text box, type ConApp2. In the Location text box, type C:\Test2 location.</li> <li>Click Add to Solution.</li> <li>On the Project menu, click Add Reference. On the Projects tab, click Select. Click OK to add the reference to the MyLibrary project.</li> <li>On the Project menu, click Set as StartUp Project to set ConApp2 as the startup project.</li> <li>In Solution Explorer, click the MyLibrary reference for the ConApp2 project, set the Copy Local property for the reference to False. This step deletes the copy of the MyLibrary.dll file that has been copied to the local Bin folder of ConApp2.</li> <li>In Solution Explorer, double-click Class1.cs to open the code window of the default class for the ConApp2 project.</li> <li> Add the following code to the Main method of ConApp2: MyLibrary.Class1 o = new MyLibrary.Class1; o.MyMethod; </li> <li>On the Build menu, click Build ConApp2.

The build succeeds.</li> <li> Open Class1 in the MyLibrary project, and then delete one of the following lines of code: Console.Write(&quot;.&quot;); Save Class1. </li> <li>Repeat step 11.

You receive one of the error messages that is described in the &quot;Symptoms&quot; section of this article.</li></ol>

<div class="references_section">

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

311491 The &quot;could not copy temporary files to the output directory&quot; error is generated in Visual Studio .NET

Additional query words: build project reference

Keywords: kberrmsg kbtshoot kbbug kbfix kbprod2web kbvs2002sp1sweep kbvs2005swept kbvs2005doesnotapply KB313512

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.