How to build all projects in one single folder? - c#

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.

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.

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:
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>
<OutDir>$(OutputPath)</OutDir>
<IntermediateOutputPath>
$(SolutionDir)\obj\$(Configuration)\$(MSBuildProjectName)\
</IntermediateOutputPath>
<UseCommonOutputDirectory>False</UseCommonOutputDirectory>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
</Project>
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" />

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

For each project:
Go into the project properties, in the "Build" tab.
Choose "All configurations", "all platforms", just in-case.
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.

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)

Related

How to copy project's resources folder to output directory in project with a reference to it

I have multiple projects in my solution. One of the projects has resources files (images). Then I have another project with unit tests. I'd like to have all these files available in unit tests project when the project with resources is referenced.
I've tried two solutions:
a) Apply a post-build script to the project with resources
The script copies resources directory content to the output (Debug/Release) directory as I need. But the script isn't executed when unit tests project is build (only when the project with resources is)
b) Set "Copy to Output Directory" property to value "Copy always" to all resources files
This works as I need even in project that has reference but I have to set this property manually to all the files. I wonder whether there is a way how to manage the same result but with a folder (to automatically copy all its content)
My question is: Is there any way how to automatically copy project's resources directory and its content to another project when the project with resources files is referenced?
You can check this link.
https://github.com/dotnet/project-system/issues/3203
As the link describes, if files are added to the directory during the build process, you must use a target to expand the wildcard, or it will only copy the files that existed at the start of the build.
You can use the following methods:
Open the *.vcxproj file and add the following code.
<ItemGroup>
<None Include="$(SolutionDir)config\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
Hope it can help you.

Reference code files from external folder

I try to reference a collection of c# code to a console project in visual studio.
I want to keep the referenced code outside the console project folder.
I want the code to automatically update when it gets changed in the external position.
The referenced codes folder structure should stay intact. (~100 files)
Is there a way of referencing/linking the code without updating everytime? "Include in Project" only works if the code is inside a solution folder.
You can add a "link" to code files outside of your project. This doesn't make a copy of the files.
Right-click your project -> Add -> Existing Item..., and browse to the file(s). Then click the down-arrow on the "Add" button and select "Add As Link":
Linked files appear with a blue arrow in the Solution Explorer:
If you want to reference an entire folder structure of code, you'll need to edit your .csproj. Something like:
<ItemGroup>
<Compile Include="..\SomeDir\**\*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)"/>
</ItemGroup>
Adjust ..\SomeDir to be the path to your code. The **\*.cs is of course a pattern to recursively include all .cs files. %(RecursiveDir), %(Filename), and %(Extension) are MSBuild placeholders.
I organise my git repos in a flat structure:
core
shared1
website1
website2
Where websites 1 and 2 both reference core and shared1.
When I add project references to core and shared1 from websites 1 and 2, they remain intact because they are both in the same relative location from the point of view of the websites.
Additionally, I organise my solution to mirror the external repo configuration, e.g.
core (solution folder)
corelibrary.csproj
shared1 (solution folder)
sharedlibrary.csproj
website1services.csproj
website1.csproj
I found a good solution, but I am curious if there is a better one. I am on Windows and using a simbolic link
cmd /c mklink /d /j _LinkedCode ..\..\..\_Code
then I can use "Include in Project" in visual studio

How can I fetch a file to be read in StreamReader and listed in a ListBox? [duplicate]

Task is to form Visual Studio 2010 project so, that during any build or publish some foo.exe utility should be copied to output (bin) directory.
Early I have made PostBuildEvent task in .csproj (MSBuild-file):
<PropertyGroup>
<PostBuildEvent>
Copy "$(SolutionDir)Tools\foo.exe" "$(ProjectDir)$(OutDir)foo.exe"
</PostBuildEvent>
</PropertyGroup>
But this is not universal. During publishing (Visual Studio 2010) foo.exe appears in bin directory, but is not copied to output publish directory. Maybe I do everything completely wrong and there is standard mechanism to include files in projects to be later, during build or publish, copied to bin?
There is and it is not dependent on post build events.
Add the file to your project, then in the file properties select under "Copy to Output Directory" either "Copy Always" or "Copy if Newer".
See MSDN.
I only have the need to push files during a build, so I just added a Post-build Event Command Line entry like this:
Copy /Y "$(SolutionDir)Third Party\SomeLibrary\*" "$(TargetDir)"
You can set this by right-clicking your Project in the Solution Explorer, then Properties > Build Events
In Solution Explorer, please select files you want to copied to output directory and assign two properties:
- Build action = Content
- Copy to Output Directory = Copy Always
This will do the trick.
Add the file to your project.
Go to the Properties of that file.
Set "Build Action" to Embedded Resource.
Set "Copy to Output Directory" to Copy Always.
There is another way that can copy items that are "outside" the Solution (which also makes it technically possible to copy Solution Items as well).
In Solution Explorer, right-click in your project and choose "Add... Existing Item". Locate the file in question (it can by any type, not just code), and next to the "Add" button, click the drop-down arrow and select "Add As Link".
In Solution Explorer, select the item that was just added to your project and change the Copy to Output Directory property to Copy if newer or Copy always, as appropriate.
In my case, setting Copy to Output Directory to Copy Always and Build did not do the trick, while Rebuild did.
Hope this helps someone!
Try adding a reference to the missing dll's from your service/web project directly. Adding the references to a different project didn't work for me.
I only had to do this when publishing my web app because it wasn't copying all the required dll's.
Just so my fellow neuronically impaired comrades might chance upon it here, I had assumed that, for web projects, if the linked file was an external .config file that the "output directory" would be the same directory that web.config lives in, i.e. your web project's root. In retrospect, it is entirely unsurprising that it copies the linked file into the root/bin folder.
So, if it's an appSettings include file, your web.config's open tag would be
<appSettings file=".\bin\includedAppSettingsFile.config">
Duh.

How can I redirect the "bin" and "obj" directories to a different location?

Is there a way to tell Visual Studio to use a different location for the bin and obj directories?
For example, if my project is in C:\my\myprojects.csproj, how can I have the obj and bin directories in, say, D:\otherdirectory\bin and D:\otherdirectory\obj. The Visual Studio project option offer only to redirect the bin directory, not the obj directory.
Also, bonus question: Can I use environment variables, not full or relative paths?
Is this possible?
Refer to this article and use the nodes BaseOutputPath (for the bin folder) and BaseIntermediateOutputPath (for the obj folder) in the .proj file.
Given below is a way to modify your debug and release folders relative to bin -
In Solution Explorer, select the C# project you want to configure
build parameters on.
Next, from the Visual Studio menu bar, select Project → Properties. The Property Pages dialog will appear for your project.
Choose the Configuration (Release/Debug) you want to change and expand the Configuration Properties node in the left hand pane. Select the Studio is placed in the "Output path" attribute of the Outputs
property sheet.
Be aware that the output path is specified separately for each kind of
build configuration, and that setting it on one configuration doesn't
set it on all the remaining ones.
Original source - http://www.eggheadcafe.com/software/aspnet/32040244/how-to-change-the-obj-folder.aspx
To move obj directories out of your code base to another common folder you can do the following. Create Directory.Build.props in the root directory of your solution with the following content:
<Project>
<PropertyGroup>
<BaseIntermediateOutputPath>$(SolutionDir)\_Obj\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
</PropertyGroup>
</Project>
To keep folder structure in your common obj directory the same as in your solution you can create files with the same and similar content in every subfolder of your solution. E.g. If you have subfolder Algorithms which contains several projects you can put file with the following content into it:
<Project>
<PropertyGroup>
<BaseIntermediateOutputPath>$(SolutionDir)\_Obj\Algorithms\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
</PropertyGroup>
</Project>
Use BaseOutputPath for bin folder.
More info on Directory.Build.props
More info on MSBuild macroses
In the Project-options in the tab Build you can choose the output path (in the bottom-section).
In Visual Studio 2013, this is specified in project "Configuration Properties/General/Intermediate Directory".

C# SLN file approach in Eclipse (Java)

How does Eclipse handle excluding Java files in the project??
In C# the list of files in the project is handled in the sln file - There seems nothing similar in Eclipse!!
Any ideas?
In Visual Studio files for C# projects are stored in the .csproj files. Solutions are just containers for projects (which can be C#/C++/VB/... projects).
The last time I worked with Eclipse all files beneath the project's root were automatically included. When one was excluded from the build the project's .classpath file was modified:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="ExcludedFile.java" kind="src" path="src"/>
<!-- ... other entries ... -->
</classpath>
You can exclude a file in Eclipse by right-clicking it in the Package Explorer and then choosing Build Path -> Exclude.
Project specific meta information (like this) is stored in either the .project or the. .classpath files in the root of the project. These are hidden in the project view, but visible in the navigator view.
You shouldn't need to directly edit the .project and .classpath files mentioned in the other answers. As with Visual Studio, the IDE generates and owns these project meta data files for you.
For example, you can exclude an individual java file from the build path by right clicking on it in the project explorer and selecting "Build path / Exclude". That will make the required .classpath changes for you.

Categories