Редагувати

Поділитися через


Change the build output directory

You can specify the location of output generated by your project on a per-configuration basis (for debug, release, or both). With default settings, output folders vary by project type and SDK, with some projects using platform-specific or framework-specific subfolders.

Visual Studio provides properties that let you place intermediate and final output in custom folders either by specifying the paths relative to the project folder, or an absolute path anywhere in the filesystem. In addition to settings you specify in the Visual Studio project properties, you can also customize output further by editing the project file and using the capabilities of MSBuild and the .NET SDK for complete control over all aspects of project output, either for individual projects or at the solution level for many projects.

Change the build output directory

  1. Clean the project or solution to remove any existing output files (Build > Clean solution).

  2. To open the project's property pages, right-click on the project node in Solution Explorer and select Properties.

  3. Select the appropriate tab based on your project type:

    • For C#, select the Build tab.
    • For Visual Basic, select the Compile tab.
    • For C++ or JavaScript, select the General tab.
  4. In the configuration drop-down at the top, choose the configuration whose output file location you want to change (Debug, Release, or All Configurations).

  5. Find the output path entry on the page—it differs depending on your project type:

    • Output path for C# and JavaScript projects
    • Build output path for Visual Basic projects
    • Output directory for Visual C++ projects

    Type in the path to generate output to (absolute, or relative to the root project directory), or choose Browse to browse to that folder.

    Output path property for a Visual Studio C# project

    Note

    Some projects (.NET and ASP.NET Core) will by default include the framework version or runtime identifier in the build path. To change this, right-click the project node in Solution Explorer, select Edit Project File, and add the following:

    <PropertyGroup>
      <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
      <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
    </PropertyGroup>
    

Tip

If the output is not being generated to the location that you specified, make sure you're building the corresponding configuration (for example, Debug or Release) by selecting it on the menu bar of Visual Studio.

Build configuration picker in Visual Studio 2019.

Build to a common output directory

By default, Visual Studio builds each project in a solution in its own folder inside the solution. You can change the build output paths of your projects to force all outputs to be placed in the same folder.

To place all solution outputs in a common directory

  1. Clean the solution to remove any existing output files (Build > Clean solution).

  2. Click on one project in the solution.

  3. On the Project menu, click Properties.

  4. In each project, depending on its type, select either Compile or Build, and set the Output path or Base output path to a folder to use for all projects in the solution.

  5. Open the project file for the project, and add the following property declaration to the first property group.

    <PropertyGroup>
      <!-- existing property declarations are here -->
      <UseCommonOutputDirectory>true</UseCommonOutputDirectory>
    </PropertyGroup>
    

    Setting UseCommonOutputDirectory to true tells Visual Studio and its underlying build engine (MSBuild) that you're putting multiple project outputs in the same folder, and so MSBuild omits the copying step that normally happens when projects depend on other projects.

  6. Repeat steps 2-5 for all projects in the solution. You can skip some projects if you have some exceptional projects that should not use the common output directory.

To set the intermediate output directory for a project (.NET projects)

  1. Clean the project to remove any existing output files.

  2. Open the project file.

  3. Add the following property declaration to the first property group.

    <PropertyGroup>
      <!-- existing property declarations are here -->
      <IntermediateOutputPath>path</IntermediateOutputPath>
    </PropertyGroup>
    

    The path is relative to the project file, or you can use an absolute path. If you want to put the project name in the path, you can reference it by using the MSBuild properties $(MSBuildProjectName), $(MSBuildProjectDirectory). For more properties you can use, see MSBuild reserved and well-known properties.

  4. Visual Studio still creates the obj folder under the project folder when you build, but it's empty. You can delete it as part of the build process. One way to do that is to add a post-build event to run the following command:

    rd "$(ProjectDir)obj" /s /q
    

    See Specify custom build events.

In Visual Studio 2022, there are different Project Designer user interfaces, depending on your project type. C# .NET Framework and all Visual Basic projects use the legacy .NET Project Designer, but C# .NET Core (and .NET 5 and later) projects use the current .NET Project Designer. C++ projects use their own property pages user interface. The steps in this section depend on what Project Designer you're using.

Change the build output directory

This procedure applies to C# projects in Visual Studio 2022 targeting .NET Core or .NET 5 and later.

  1. Clean the project or solution to remove any existing output files (Build > Clean solution).

  2. Right-click on the project node in Solution Explorer and select Properties.

  3. Expand the Build section and scroll down to the Output subsection.

  4. Find the Base output path for C#, and type in the path to generate output to (absolute or relative to the root project directory), or choose Browse to browse to that folder instead. Note that the configuration name is appended to the base output path to generate the actual output path.

    Screenshot that shows the Output path property for a .NET Core C# project.

    Note

    Some projects (.NET and ASP.NET Core) will by default include the framework version or runtime identifier in the final output path. To change this, right-click the project node in Solution Explorer, select Edit Project File, and add the following:

    <PropertyGroup>
      <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
      <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
    </PropertyGroup>
    

Build to a common output directory

By default, Visual Studio builds each project in a solution in its own folder inside the solution. You can change the build output paths of your projects to force all outputs to be placed in the same folder.

  1. Clean the solution to remove any existing output files (Build > Clean solution).

  2. Click on one project in the solution.

  3. On the Project menu, click Properties.

  4. In each project, depending on its type, select either Compile or Build, and set the Output path or Base output path to a folder to use for all projects in the solution.

  5. Open the project file for the project, and add the following property declaration to the first property group.

    <PropertyGroup>
      <!-- existing property declarations are here -->
      <UseCommonOutputDirectory>true</UseCommonOutputDirectory>
    </PropertyGroup>
    

    Setting UseCommonOutputDirectory to true tells Visual Studio and its underlying build engine (MSBuild) that you're putting multiple project outputs in the same folder, and so MSBuild omits the copying step that normally happens when projects depend on other projects.

  6. Repeat steps 2-5 for all projects in the solution. You can skip some projects if you have some exceptional projects that should not use the common output directory.

Tip

If the output is not being generated to the location that you specified, make sure you're building the corresponding configuration (for example, Debug or Release) by selecting it on the menu bar of Visual Studio.

Screenshot of the Build configuration picker in Visual Studio 2022.

Set the intermediate output directory for a project

This procedure applies to C# projects in Visual Studio 2022 targeting .NET Core or .NET 5 and later.

  1. Clean the project to remove any existing output files.

  2. Open the project file.

  3. Add the following property declaration to the first property group.

    <PropertyGroup>
      <!-- existing property declarations are here -->
      <IntermediateOutputPath>path</IntermediateOutputPath>
    </PropertyGroup>
    

    The path is relative to the project file, or you can use an absolute path. If you want to put the project name in the path, you can reference it by using the MSBuild properties $(MSBuildProjectName), $(MSBuildProjectDirectory). For more properties you can use, see MSBuild reserved and well-known properties.

  4. Visual Studio still creates the obj folder under the project folder when you build, but it's empty. You can delete it as part of the build process. One way to do that is to add a post-build event to run the following command:

    rd "$(ProjectDir)obj" /s /q
    

    See Specify custom build events.

Use Directory.Build.props to set the output directory

If you have a large number of projects, and you want to change the output folder for them all, it would be tedious and error-prone to change each one using the methods described earlier in this article. In such cases, you can create a file in the solution folder, Directory.Build.props, to set the appropriate MSBuild properties in one place, to apply to all the projects in the solution. By placing a file with this particular name in the parent folder of all the projects you want to be affected, you can easily maintain customizations in a single place and make it easy to change the values. See Customize the build by folder.

Use artifacts to organize build output

For .NET 8 projects, you can use the artifacts functionality to arrange output in a highly customizable and flexible way. See Artifacts output layout.