45

If a library (eg, on github) doesn't distribute itself via a nuget package, I'd have to manually include it as a reference, correct? I see a lot of reference posts for how to add a reference to a project for Visual Studio, but I can't seem to figure out how to do it on Visual Studio Code.

In this case, I've downloaded the library's zip, and moved the expanded folder into my project, and then tried using <namespace>, which did not work.

EDIT:

I noticed that this downloaded zip contained a .nuspec. Is there something I can do with this file extension to import it in my project?

ruffin
  • 16,507
  • 9
  • 88
  • 138
user6728767
  • 1,123
  • 3
  • 15
  • 31
  • Try this SO answer: https://stackoverflow.com/questions/42000798/how-to-add-assembly-references-in-visual-studio-code -- the first answer adds a nuget package, but others after address adding references without nuget packages. – user7396598 Apr 12 '18 at 22:03

4 Answers4

101

Let's say you have two projects:

1) Project1.Api

2) Project2.Executable


Command line syntax for dotnet add reference:

    cd Project2.Executable
    dotnet add reference ../Project1.Api/Project1.Api.csproj

If you check the Project2.Executable.csproj file, you will see the following entry:

    <ItemGroup>
         <ProjectReference Include = "..\Project1.Api\Project1.Api.csproj" />
    </ItemGroup>
ruffin
  • 16,507
  • 9
  • 88
  • 138
  • 1
    For anyone who bangs his head, cuz this ain't seem to work: The steps are correct,but be careful NOT to add unreadable(invisible) white space chars such as tabs, CR-LF, while editing project file ... This may prevent your VSCode to read the project file correctly. Happens regularly to me. – Sold Out Feb 04 '22 at 12:34
  • How can I use a package, e.g. NLog that is added as a reference in Project1.Api? – Ali Majed HA Nov 22 '22 at 16:46
27

Add "vscode-solution-explorer" Extension. It will folder structure as visual studio. Right click on project --> Add Reference --> Select the reference project from the list.

Avinash T H
  • 511
  • 5
  • 5
  • Thanx man ! Really helped. The plugin did exactly what I did in my .csproj file, but without unreadable whitespace. Suddenly it started to work ! I recall, there used to be some problem with wrong white-space in .csproj file. (Compare working and failing version) You may accidentally add such whitespace when editing manualy. Hence, VSCode can not read lines of the project file properly. This extension does not add the spurious whitespace, so it works :) – Sold Out Feb 04 '22 at 12:29
  • Thank you, I just learned of this extension from your answer. I immediately consider it to be indispensable. – M.Bearden Apr 28 '23 at 22:00
9

You can open .csproj file of the project you want to add reference to and add project reference like this:

<ItemGroup>
    <ProjectReference Include = "<RELATIVE_PATH_TO_REFERENCE_PROJECT>" />
</ItemGroup>

If the ItemGroup for ProjectReference already exist then you can just add to it.

Example:

<ItemGroup>
    <ProjectReference Include = "../MyLibrary.csproj" />
</ItemGroup>
kodebot
  • 1,718
  • 1
  • 22
  • 29
  • 1
    If I update the referenced csproj file, that change is not reflected in the project where it is referenced. – Luniam May 12 '22 at 07:14
-5

In visual studio, in the solution explorer, expand the project that will reference this other library. You will see "References", right click and choose "Add". Then choose browse on the left. Find your dll in your file system. If vs can't find the library you may need to unzip it. I've read where you may need to copy the dll into the bin folders, I recommend trying it without doing that, then copying it in to them if it fails without them.

Btw Googling "visual studio add reference" comes up with A LOT of great results.

user7396598
  • 1,269
  • 9
  • 6