简体   繁体   中英

What's a recommended/safe way to compile projects in a solution into a folder hierarchy?

I sometimes create C#-based software that consists of multiple projects inside one solution. The assemblies created from the projects should go into different output directories.

As a simple example, consider this:

myApplication
 |- MainApplication.exe
 |- PluginInterfaces.dll
 \- plugins
     \- SamplePlugin.dll

That would be the result of a solution with three projects inside.

In former versions of Visual Studio (I am not exactly sure when the switch happened - maybe from VS 2019 to VS 2022, as VS 2022 was the first version where I became aware of the issue?), I could use the project settings to set an output path for each project.

As the output path contained the build configuration, I had to set this output path for each project and each build configuration, which was slightly cumbersome, but still, it would get the job done. In the above example, this would result in the following settings:

Project Debug Output Path Release Output Path
MainApplication ..\..\bin\Debug\ ..\..\bin\Release\
PluginInterfaces ..\..\bin\Debug\ ..\..\bin\Release\
SamplePlugin ..\..\bin\Debug\plugins\ ..\..\bin\Release\plugins\

Now, I just became aware this looks a bit different in VS 2022 - there now is a single, configuration-agnostic setting called "Base Output Path".

This way, the build configuration (and target framework) will be added automatically after the base output path. While this is somewhat more convenient, I now face the problem that it makes no sense anymore to assign different base output paths to individual projects, unless I want to have the resulting assemblies end up in completely separate folder trees.


If there is no setting in VS to work around that, I presume I could edit the .csproj file to somehow directly influence the output path - ways to do this are mentioned in another answer , though its author cautions that it may have undesirable side-effects for the build-logic.

FWIW, I have actually tried modifying the <OutputPath> like this:

<OutputPath>$(BaseOutputPath)$(PlatformName)\$(Configuration)\plugins\</OutputPath>

Unfortunately, the platform for some reason gets appended after that path. Same if I use <OutDir> instead of <OutputPath> .

Another possible approach could be just compiling to the default locations, then using a post-build event to copy everything to the final location - but then, I fear launching the application from VS for debugging would not take the assemblies from their final (copied to) location.


What is an effective way to configure my projects so the output forms a folder tree, while not risking to run into any side-effects by circumventing default behaviour?

It appears I can achieve the desired result with the following settings in my .csproj file:

<BaseOutputPath>..\..\bin\</BaseOutputPath>
<OutDir>$(BaseOutputPath)\$(Configuration)\$(TargetFramework)\plugins\</OutDir>

I am not sure why it works like this - looking at the source , I had actually expected also having to specify

<AppendTargetFrameworkToOutputPath>False</AppendTargetFrameworkToOutputPath>

but apparently, that was not necessary - the additional targt framework mention after \plugins\ is gone already once I force the target framework to appear in front of it.

(Now, I am not yet sure this does not provoke any undesirable side-effects, so I'm going to leave this answer open for a while rather than marking it as accepted right away.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM