dotnet 命令dotnet command

本主题适用于:✓ .NET Core SDK 1.x .NET Core SDK 2.xThis topic applies to: ✓ .NET Core SDK 1.x .NET Core SDK 2.x

nameName

dotnet - 一款管理 .NET 源代码和二进制文件的工具。dotnet - A tool for managing .NET source code and binaries.

摘要Synopsis

dotnet [command] [arguments] [--additional-deps] [--additionalprobingpath] [-d|--diagnostics] [--fx-version]
    [-h|--help] [--info] [--list-runtimes] [--list-sdks] [--roll-forward-on-no-candidate-fx] [-v|--verbosity] [--version]

描述Description

dotnet 是一款管理 .NET 源代码和二进制文件的工具。dotnet is a tool for managing .NET source code and binaries. 它公开执行特定任务的命令,例如 dotnet builddotnet runIt exposes commands that perform specific tasks, such as dotnet build and dotnet run. 每个命令都定义自己的参数。Each command defines its own arguments. 在每个命令后键入 --help 以访问简要帮助文档。Type --help after each command to access brief help documentation.

可以使用 dotnet 来运行应用程序,方法是指定应用程序 DLL,如 dotnet myapp.dll.dotnet can be used to run applications, by specifying an application DLL, such as dotnet myapp.dll. 要了解部署选项,请参阅 .NET Core 应用程序部署See .NET Core application deployment for to learn about deployment options.

选项Options

--additional-deps <PATH>

其他 deps.json 文件的路径。Path to additional deps.json file.

--additionalprobingpath <PATH>

包含要进行探测的探测策略和程序集的路径。Path containing probing policy and assemblies to probe.

-d|--diagnostics

启用诊断输出。Enables diagnostic output.

--fx-version <VERSION>

用于运行应用程序的 .NET Core 运行时版本。Version of the .NET Core runtime to use to run the application.

-h|--help

打印出给定命令的文档,如 dotnet build --helpPrints out documentation for a given command, such as dotnet build --help. dotnet --help 打印可用命令列表。dotnet --help prints a list of available commands.

--info

打印出有关 .NET Core 安装和计算机环境(如当前操作系统)的详细信息,并提交 .NET Core 版本的 SHA。Prints out detailed information about a .NET Core installation and the machine environment, such as the current operating system, and commit SHA of the .NET Core version.

--list-runtimes

显示已安装的 .NET Core 运行时。Displays the installed .NET Core runtimes.

--list-sdks

显示已安装的 .NET Core SDK。Displays the installed .NET Core SDKs.

--roll-forward-on-no-candidate-fx

如果设置为 0,则禁用次要版本前滚。Disables minor version roll forward, if set to 0. 有关详细信息,请参阅前滚For more information, see Roll forward.

-v|--verbosity <LEVEL>

设置命令的详细级别。Sets the verbosity level of the command. 允许使用的值为 q[uiet]m[inimal]n[ormal]d[etailed]diag[nostic]Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]. 并非在每个命令中均受支持;请参阅特定的命令页,确定此选项是否可用。Not supported in every command; see specific command page to determine if this option is available.

--version

打印使用中的 .NET Core SDK 版本。Prints out the version of the .NET Core SDK in use.

dotnet 命令dotnet commands

常规General

命令Command 函数Function
dotnet builddotnet build 生成 .NET Core 应用程序。Builds a .NET Core application.
dotnet build-serverdotnet build-server 与通过生成启动的服务器进行交互。Interacts with servers started by a build.
dotnet cleandotnet clean 清除生成输出。Clean build outputs.
dotnet helpdotnet help 显示命令更详细的在线文档。Shows more detailed documentation online for the command.
dotnet migratedotnet migrate 将有效的预览版 2 项目迁移到 .NET Core SDK 1.0 项目。Migrates a valid Preview 2 project to a .NET Core SDK 1.0 project.
dotnet msbuilddotnet msbuild 提供对 MSBuild 命令行的访问权限。Provides access to the MSBuild command line.
dotnet newdotnet new 为给定的模板初始化 C# 或 F# 项目。Initializes a C# or F# project for a given template.
dotnet packdotnet pack 创建代码的 NuGet 包。Creates a NuGet package of your code.
dotnet publishdotnet publish 发布 .NET 依赖于框架或独立应用程序。Publishes a .NET framework-dependent or self-contained application.
dotnet restoredotnet restore 还原给定应用程序的依赖项。Restores the dependencies for a given application.
dotnet rundotnet run 从源运行应用程序。Runs the application from source.
dotnet slndotnet sln 用于添加、删除和列出解决方案文件中项目的选项。Options to add, remove, and list projects in a solution file.
dotnet storedotnet store 将程序集存储到运行时包存储区。Stores assemblies in the runtime package store.
dotnet testdotnet test 使用测试运行程序运行测试。Runs tests using a test runner.

项目引用Project references

命令Command 函数Function
dotnet add referencedotnet add reference 添加项目引用。Adds a project reference.
dotnet list referencedotnet list reference 列出项目引用。Lists project references.
dotnet remove referencedotnet remove reference 删除项目引用。Removes a project reference.

NuGet 包NuGet packages

命令Command 函数Function
dotnet add packagedotnet add package 添加 NuGet 包。Adds a NuGet package.
dotnet remove packagedotnet remove package 删除 NuGet 包。Removes a NuGet package.

NuGet 命令NuGet commands

命令Command 函数Function
dotnet nuget deletedotnet nuget delete 从服务器删除或取消列出包。Deletes or unlists a package from the server.
dotnet nuget localsdotnet nuget locals 清除或列出本地 NuGet 资源,例如 http 请求缓存、临时缓存或计算机范围的全局包文件夹。Clears or lists local NuGet resources such as http-request cache, temporary cache, or machine-wide global packages folder.
dotnet nuget pushdotnet nuget push 将包推送到服务器,并将其发布。Pushes a package to the server and publishes it.

全局工具命令Global Tools commands

.NET Core 全局工具可与 .NET Core SDK 2.1.300 一起开始使用:.NET Core Global Tools are available starting with .NET Core SDK 2.1.300:

命令Command 函数Function
dotnet tool installdotnet tool install 在计算机上安装全局工具。Installs a Global Tool on your machine.
dotnet tool listdotnet tool list 列出当前安装在计算机上的默认目录中或指定路径中的所有全局工具。Lists all Global Tools currently installed in the default directory on your machine or in the specified path.
dotnet tool uninstalldotnet tool uninstall 从计算机中卸载全局工具。Uninstalls a Global Tool from your machine.
dotnet tool updatedotnet tool update 在计算机上更新全局工具。Updates a Global Tool on your machine.

其他工具Additional tools

自 .NET Core SDK 2.1.300 开始,许多使用 DotnetCliToolReference 的仅在每个项目的基础上可用的工具现作为 .NET Core SDK 的一部分提供。Starting with .NET Core SDK 2.1.300, a number of tools that were available only on a per project basis using DotnetCliToolReference are now available as part of the .NET Core SDK. 下表中列出了这些工具:These tools are listed in the following table:

工具Tool 函数Function
dev-certsdev-certs 创建和管理开发证书。Creates and manages development certificates.
efef Entity Framework Core 命令行工具。Entity Framework Core command-line tools.
sql-cachesql-cache SQL Server 缓存命令行工具。SQL Server cache command-line tools.
user-secretsuser-secrets 管理开发用户机密。Manages development user secrets.
watchwatch 启动文件观察程序,以在更改文件时运行命令。Starts a file watcher that runs a command when files change.

有关每个工具的详细信息,请键入 dotnet <tool-name> --helpFor more information about each tool, type dotnet <tool-name> --help.

示例Examples

创建新的 .NET Core 控制台应用程序:Creates a new .NET Core console application:

dotnet new console

还原给定应用程序的依赖项:Restore dependencies for a given application:

dotnet restore

备注

从 .NET Core 2.0 SDK 开始,无需运行 dotnet restore,因为它由所有需要还原的命令隐式运行,如 dotnet newdotnet builddotnet runStarting with .NET Core 2.0 SDK, you don't have to run dotnet restore because it's run implicitly by all commands that require a restore to occur, such as dotnet new, dotnet build and dotnet run. 在执行显式还原有意义的某些情况下,例如 Azure DevOps Services 中的持续集成生成中,或在需要显式控制还原发生时间的生成系统中,它仍然是有效的命令。It's still a valid command in certain scenarios where doing an explicit restore makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control the time at which the restore occurs.

生成给定目录中的项目及其依赖项:Build a project and its dependencies in a given directory:

dotnet build

运行应用程序 DLL,如 myapp.dllRun an application DLL, such as myapp.dll:

dotnet myapp.dll

环境变量Environment variables

DOTNET_PACKAGES

主包缓存。The primary package cache. 如果未设置,则默认为 Unix 上的 $HOME/.nuget/packages 或 Windows 上的 %HOME%\NuGet\PackagesIf not set, it defaults to $HOME/.nuget/packages on Unix or %HOME%\NuGet\Packages on Windows.

DOTNET_SERVICING

指定加载运行时期间共享主机要使用的服务索引的位置。Specifies the location of the servicing index to use by the shared host when loading the runtime.

DOTNET_CLI_TELEMETRY_OPTOUT

指定是否收集并向 Microsoft 发送 .NET Core 工具使用情况的相关数据。Specifies whether data about the .NET Core tools usage is collected and sent to Microsoft. 设置为 true 以选择退出遥测功能(接受的值为 true1yes)。Set to true to opt-out of the telemetry feature (values true, 1, or yes accepted). 否则,设置为 false 以选择加入遥测功能(接受的值为 false0no)。Otherwise, set to false to opt into the telemetry features (values false, 0, or no accepted). 如果未设置,则默认为 false 且遥测功能为活动状态。If not set, the default is false and the telemetry feature is active.

DOTNET_MULTILEVEL_LOOKUP

指定是否从全局位置解析 .NET Core 运行时、共享框架或 SDK。Specifies whether .NET Core runtime, shared framework, or SDK are resolved from the global location. 如果未设置,则默认为 trueIf not set, it defaults to true. 设置为 false 不从全局位置解析,并且具有独立的 .NET Core 安装(接受的值为 0false)。Set to false to not resolve from the global location and have isolated .NET Core installations (values 0 or false are accepted). 有关多级别查找的详细信息,请参阅 Multi-level SharedFX Lookup(多级别 SharedFX 查找)。For more information about multi-level lookup, see Multi-level SharedFX Lookup.

DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX

如果设置为 0,则禁用次要版本前滚。Disables minor version roll forward, if set to 0. 有关详细信息,请参阅前滚For more information, see Roll forward.