Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference code files from external folder

I try to reference a collection of c# code to a console project in visual studio.

  1. I want to keep the referenced code outside the console project folder.

  2. I want the code to automatically update when it gets changed in the external position.

  3. 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.

like image 204
Mechandrius Avatar asked Oct 24 '25 22:10

Mechandrius


1 Answers

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":

Add As Link

Linked files appear with a blue arrow in the Solution Explorer:

Linked file in 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.

like image 71
canton7 Avatar answered Oct 26 '25 12:10

canton7