简体   繁体   English

如何在 C# 项目中使用 windows 命令行安装 Nuget 包?

[英]How to install Nuget package using windows command line in C# project?

I have downloaded NuGet version - 5.1.0 and tried to install one of the package - log4net using cmd.我已经下载了 NuGet 版本 - 5.1.0 并尝试使用 cmd 安装包之一 - log4net。 it failed.它失败了。 below is error -以下是错误 -

在此处输入图片说明

Here, ClassLibrary1 is C# project where I have to add log4net package.这里,ClassLibrary1 是 C# 项目,我必须在其中添加 log4net 包。 I don't want to use NuGet Package Manager Console option available in Visual Studio.我不想使用 Visual Studio 中提供的 NuGet 包管理器控制台选项。

Did I write correct command?我写的命令正确吗?

Install-Package is how you install via Visual Studio. Install-Package 是您通过 Visual Studio 安装的方式。 The cmdlet to install a package via the CLI is nuget install <Package-Name> .通过 CLI 安装包的 cmdlet 是nuget install <Package-Name>

Please see this Microsoft Doc for reference: https://docs.microsoft.com/en-us/nuget/consume-packages/install-use-packages-nuget-cli请参阅此 Microsoft Doc 以供参考: https : //docs.microsoft.com/en-us/nuget/consume-packages/install-use-packages-nuget-cli

For those that want the package added to their project file (rather than a bunch of files in a directory) use:对于那些希望将包添加到他们的项目文件(而不是目录中的一堆文件)的人,请使用:

dotnet add package <PACKAGE_NAME>

this will only work if you have the dotnet command-line tool installed (obtained by installing the .NET Core SDK).这仅在您安装了 dotnet 命令行工具(通过安装 .NET Core SDK 获得)时才有效。

More info can be found here: https://docs.microsoft.com/en-us/nuget/consume-packages/install-use-packages-dotnet-cli更多信息可以在这里找到: https : //docs.microsoft.com/en-us/nuget/consume-packages/install-use-packages-dotnet-cli

There are three official ways to install NuGet package in your solutions.官方提供了三种在解决方案中安装 NuGet 包的方法。

在此处输入图片说明

  1. Using Package Manager Console;使用包管理器控制台; It includes in visual studio ide (currently only on window platform) or install nuget command line tools from nuget gallery.You can copy and paste required nuget package in package manager console in ide which starts with "Install-Package" or in command line with "nuget install ".它包含在 Visual Studio ide(目前仅在窗口平台上)或从 nuget 库安装 nuget 命令行工具。您可以在以“Install-Package”开头的 ide 中的包管理器控制台中复制并粘贴所需的 nuget 包,或在命令行中使用“nuget 安装”。 This will add packagereference in csproj这将在 csproj 中添加 packagereference

  2. Using .Net CLI;使用.Net CLI; You need to install .net command line tool which install together with .net sdk.您需要安装与 .net sdk 一起安装的 .net 命令行工具。 So, you just need to copy and paste in command line or bash or terminal.因此,您只需要在命令行或 bash 或终端中复制和粘贴即可。 This works in linux, window, mac operating systems.这适用于 linux、window、mac 操作系统。 This will add package reference in csproj file eventually.这将最终在 csproj 文件中添加包引用。

  3. Using PackageReference;使用 PackageReference; you copy and paste reference into csproj file directly.您直接将引用复制并粘贴到 csproj 文件中。

  4. Using Paket CLI, also another kind of cli which i have never used before.使用 Paket CLI,也是我以前从未使用过的另一种 cli。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM