How do I add assembly references in Visual Studio Code?
Asked Answered
B

6

67

So I've come across a similar issue twice now while working on my first project in C#. When trying to add either using System.Data; or using System.Timers;, I get the following error:

The type or namespace name 'x' doesn't exist in the namespace 'System' (are you missing an assembly reference?).

I have tried beginning a new project and running restore to see if I had accidentally removed something in the dependencies, but upon generating a new project I still receive the same error. I have tried to research the question and have seen answers referring to the 'solutions explorer', but as far as I can see there doesn't seem to be such a feature by this name in Visual Studio Code 1.8.

Can anyone point me in the right direction for how to get these working, perhaps by manually adding into the dependencies?

Baalbeer answered 2/2, 2017 at 11:16 Comment(4)
Are you sure you're not finding help/advice relating to Visual Studio (not VS Code)? Solution Explorer is a feature in VS but I'm not sure it exists in VSC.Eubanks
@Eubanks That sounds quite plausible, I wasn't really clear on the difference. I'll try searching again with a focus on the VSC aspect.Baalbeer
@Eubanks OK, it looks like you're right regarding the absence of Solution Explorer. It seems I'd have to add missing references to the dependencies file manually, but as to where or how I'd know what version number to add, I'm at a bit of a loss.Baalbeer
#40675662Winkler
C
90

.csproj Project file

The following topic applies to .csproj project file and : .NET Core 1.x SDK, .NET Core 2.x SDK

Adds a package reference to a project file.

dotnet add package

Example

Add Newtonsoft.Json NuGet package to a project:

dotnet add package Newtonsoft.Json

.json Project file

The following topic applies to .json project file:

This guide walks you through the process of adding any assembly reference in Visual Studio Code. In this example, we are adding the assembly reference System.Data.SqlClient into .NET Core C# console application.

Note

  • At step #6, enter the assembly reference that you want.
  • Some assembly reference is applicable to .NET Framework and it will gives you error(s).
  • OleDb is not available in .NET Core, probably because it's not cross platform.

Prerequisites

  1. Install Visual Studio Code
  2. Install .NET Core SDK (Preview 2 version)
  3. Install NuGet Package Manager from the Visual Studio Code Extension Marketplace
  4. Install C# extension from Visual Studio Code Extension Marketplace

Steps

  1. Launch Visual Studio Code
  2. Open your project folder
  3. Launch VS Code Command Palette by pressing F1 or Ctrl+Shift+P or Menu Bar > View > Command Palette

enter image description here

  1. In Command Palette box, type nu

enter image description here

  1. Click on NuGet Package Manager: Add Package

  2. Enter package filter e.g. system.data (Enter your assembly reference here)

enter image description here

  1. Press Enter
  2. Click on System.Data.SqlClient

enter image description here

  1. The following prompt pops up

enter image description here

  1. Click on Restore

enter image description here

  1. The following Output panel pops up

enter image description here

  1. In the Explorer panel, click on project.json to open it

enter image description here

  1. In the Editor panel, it shows the assembly reference added into project.json file

enter image description here

  1. Assembly reference, System.Data.SqlClient used in Program.cs

enter image description here

Cytherea answered 22/2, 2017 at 18:32 Comment(5)
there is no suggestion with Nuget , do we need to install something else for that ?Funambulist
@Funambulist Yes, marketplace.visualstudio.com/…Alidia
There is no Nuget: Install/Reference command, only Add Package!Lombok
what about System.Data.OleDb? no suggestion for that in command palette.Benito
@EmonHaque: OleDb is not available in .NET Core, probably because it's not cross platform.Cytherea
A
9

Use the command dotnet add package to add a package reference to your project. For example: dotnet add package Newtonsoft.Json, which adds the package reference to the *.csproj project file:

<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />

and now you can run the command dotnet restore to restores the dependencies of your project.

Reference: dotnet add package

Anarthrous answered 8/2, 2018 at 18:42 Comment(0)
C
2

drag the dll file and drop it into the bin folder

drag the dll file and drop it into the bin folder

Cordillera answered 24/6, 2018 at 14:1 Comment(0)
C
2

Above answer from ikolim doesnt work as indicated by someone else too, there is no, Nuget: Install/Reference command. There is only Add Package! So the answer in the below link solved my problem. Manually editing the Myproject.csproj file.

Duplicate of this thread

Calamondin answered 12/9, 2018 at 11:15 Comment(0)
B
2

I've stored the files in a project folder named "dlls" and added the reference files in my .csproj file like this:

 <ItemGroup>
     <Reference Include="Microsoft.Office.Client.Policy.Portable">
      <HintPath>dlls\Microsoft.Office.Client.Policy.Portable.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Office.Client.TranslationServices.Portable">    
 <HintPath>dlls\Microsoft.Office.Client.TranslationServices.Portable.dll</HintPath>
    </Reference>
</ItemGroup>
Bother answered 5/7, 2021 at 9:54 Comment(1)
This works well on VS Code 1.67 in 2022. Having a third party refs folder is much better than dropping files into your Debug folder, which surely should be output only (& not checked into source control). Note the use of "Reference", not "PackageReference", the latter being for nuget refs only. Best answer!Wedged
G
-2

In case of extisting .dll reference, Right click project Add existing item > select path to .dll After added dll in project,right click .dll

build-action = Content, Copy-to-output-dir = Always/ or if newer

Grog answered 31/1, 2021 at 12:23 Comment(1)
vs code not visual studioBernete

© 2022 - 2024 — McMap. All rights reserved.