user3068522
user3068522

Reputation: 153

How to build all projects in one single folder?

Is there a way to build solution into a single folder? I have several projects in this solution and each access a config file that should be in the current directory. I just move each project's build files into one and it still works, however, it looks so unorganized and messy. I just want to know it there are other ways on how to do it.

Upvotes: 13

Views: 18945

Answers (5)

JSparrow
JSparrow

Reputation: 166

This MSDN article explains how to do it in a nice, DRY way: https://blogs.msdn.microsoft.com/kirillosenkov/2015/04/04/using-a-common-intermediate-and-output-directory-for-your-solution/

It allows you to specify those directories only once, and use those settings in multiple projects.

Steps:

  1. Create a single common.props file in solution, that will specify and overwrite output and intermediate paths for a project to a common directory (like Solution/bin). Here is a sample *.props file that I found linked in the article:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
            <SolutionDir>$(MSBuildThisFileDirectory)</SolutionDir>
            <Configuration Condition="$(Configuration) == ''">Debug</Configuration>
            <OutputPath>$(SolutionDir)\bin\$(Configuration)\</OutputPath>
            <OutDir>$(OutputPath)</OutDir>
            <IntermediateOutputPath>
                $(SolutionDir)\obj\$(Configuration)\$(MSBuildProjectName)\
            </IntermediateOutputPath>
            <UseCommonOutputDirectory>False</UseCommonOutputDirectory>
            <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
        </PropertyGroup>
    </Project>
    
  2. Include this file into every *.csproj that you want to set the common output dirs for, by adding this line (the actual path may differ): <Import Project="..\Common.props" />

UPDATE: Nowadays you can put the contents of common.props in Directory.Build.props file that will be automatically imported by all projects in the directory tree. Docs: https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-by-directory?view=vs-2022&source=recommendations#directorybuildprops-and-directorybuildtargets

Upvotes: 11

Hugo Leote
Hugo Leote

Reputation: 51

Note that if we're talking about building multiple executables into the same output directory, you can also add them as project references to the main (startup) project. They will be automatically copied to the main project output directory everytime you build it.

(note: this applies to .NET Core projects in VS 2017 or VS 2019. I'm not sure if it would work for .NET Framework projects)

Upvotes: 1

Sebastian.Belczyk
Sebastian.Belczyk

Reputation: 885

You can change projects "Output path", by default it's bin directory of given project.

Right click on each project, select Properties from context menu, then select Build tab. Ont the bottom in Output section change Output path:. Set same path for each project.

I agree with comments under your question, you should not change it. Instead you may create post build action (PS script) that will copy all files from project's bin directories to one designated by you.

Update:

Set this script as Post Build command (Project's properties->Build Events tab->Post build event command line):

xcopy "$(TargetDir)*" "$(SolutionDir)Build" /s /i /Y

Upvotes: 7

MaMazav
MaMazav

Reputation: 1885

For each project:

  1. Go into the project properties, in the "Build" tab.
  2. Choose "All configurations", "all platforms", just in-case.
  3. In the output folder write "..\bin\" (or any path which is uniform for all of them - not in the current project directory). Alternatively, to organize DLLs in sub-folders you can write "..\bin\Sub-project-directory" in the output path. Then you should add an App.config file for the EXE project with a probing to all DLLs so they can be found and loaded on runtime.

Upvotes: 1

FLCL
FLCL

Reputation: 2514

You can set output directory in the settings of every project in solution (if we are about Visual Studio). Menu: Project -> properties -> Build -> Output path. For example, I use ..\Build\ to build projects into Build directory of solution root.

Upvotes: 14

Related Questions