Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you share code between projects/solutions in Visual Studio?

People also ask

Can a Visual Studio solution contain multiple projects?

When you create a new project, Visual Studio creates a solution to contain the project. You can then add other new or existing projects to the solution if you want.

How do I run a project in another project in the same solution?

If you want to run two project simultaneously, you need to start another visual studio process again. The quick way is go to bin folder, right click .exe file choose open with visual studio. Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not.


You can "link" a code file between two projects. Right click your project, choose Add -> Existing item, and then click the down arrow next to the Add button:

Screengrab

In my experience linking is simpler than creating a library. Linked code results in a single executable with a single version.


A project can be referenced by multiple solutions.

Put your library or core code into one project, then reference that project in both solutions.


File > Add > Existing Project... will let you add projects to your current solution. Just adding this since none of the above posts point that out. This lets you include the same project in multiple solutions.


You can include a project in more than one solution. I don't think a project has a concept of which solution it's part of. However, another alternative is to make the first solution build to some well-known place, and reference the compiled binaries. This has the disadvantage that you'll need to do a bit of work if you want to reference different versions based on whether you're building in release or debug configurations.

I don't believe you can make one solution actually depend on another, but you can perform your automated builds in an appropriate order via custom scripts. Basically treat your common library as if it were another third party dependency like NUnit etc.


You can wild-card inline using the following technique (which is the way in which @Andomar's solution is saved in the .csproj)

<Compile Include="..\MySisterProject\**\*.cs">
  <Link>_Inlined\MySisterProject\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>

Put in:

    <Visible>false</Visible>

If you want to hide the files and/or prevent the wild-card include being expanded if you add or remove an item from a 'virtual existing item' folder like MySisterProject above.


You would simply create a separate Class Library project to contain the common code. It need not be part of any solution that uses it. Reference the class library from any project that needs it.

The only trick at all is that you will need to use a file reference to reference the project, since it will not be part of the solutions that refer to it. This means that the actual output assembly will have to be placed in a location that can be accessed by anyone building a project that references it. This can be done by placing the assembly on a share, for instance.