dotnet 命令dotnet command

本文适用于: ✔️ .NET Core 2.1 SDK 及更高版本This article applies to: ✔️ .NET Core 2.1 SDK and later versions


dotnet - .NET Core CLI 的通用驱动程序。dotnet - The generic driver for the .NET Core CLI.


获取有关可用命令和环境的信息:To get information about the available commands and the environment:

dotnet [--version] [--info] [--list-runtimes] [--list-sdks]

dotnet -h|--help

运行命令(需要 SDK 安装):To run a command (requires SDK installation):

dotnet <COMMAND> [-d|--diagnostics] [-h|--help] [--verbosity <LEVEL>]
    [command-options] [arguments]

运行应用程序:To run an application:

dotnet [--additionalprobingpath <PATH>] [--additional-deps <PATH>]
    [--fx-version <VERSION>]  [--roll-forward <SETTING>]
    <PATH_TO_APPLICATION> [arguments]

dotnet exec [--additionalprobingpath] [--additional-deps <PATH>]
    [--fx-version <VERSION>]  [--roll-forward <SETTING>]
    <PATH_TO_APPLICATION> [arguments]

--roll-forward 自 .NET Core 3.x 起可用。--roll-forward is available since .NET Core 3.x. 使用 .NET Core 2.x 的 --roll-forward-on-no-candidate-fxUse --roll-forward-on-no-candidate-fx for .NET Core 2.x.


dotnet 命令有两个函数:The dotnet command has two functions:

  • 它提供了用于处理 .NET Core 项目的命令。It provides commands for working with .NET Core projects.

    例如,dotnet build 生成项目。For example, dotnet build builds a project. 每个命令定义自己的选项和参数。Each command defines its own options and arguments. 所有命令都支持 --help 选项,用于打印有关如何使用命令的简短文档。All commands support the --help option for printing out brief documentation about how to use the command.

  • 它运行 .NET Core 应用程序。It runs .NET Core applications.

    指定应用程序 .dll 文件的路径以运行应用程序。You specify the path to an application .dll file to run the application. 运行应用程序即意味着找到并执行入口点,对于控制台应用,入口点是 Main 方法。To run the application means to find and execute the entry point, which in the case of console apps is the Main method. 例如,dotnet myapp.dll 运行 myapp 应用程序。For example, dotnet myapp.dll runs the myapp application. 要了解部署选项,请参阅 .NET Core 应用程序部署See .NET Core application deployment to learn about deployment options.


dotnet 本身有不同的选项,可用于运行命令和运行应用程序。Different options are available for dotnet by itself, for running a command, and for running an application.

dotnet 本身的选项Options for dotnet by itself

以下是 dotnet 本身的选项。The following options are for dotnet by itself. 例如 dotnet --infoFor example, dotnet --info. 这些选项打印出有关环境的信息。They print out information about the environment.

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

  • --version

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

  • --list-runtimes

    打印已安装的 .NET Core 运行时的列表。Prints out a list of the installed .NET Core runtimes. x86 版本的 SDK 只列出 x86 运行时,而 x64 版本的 SDK 只列出 x64 运行时。An x86 version of the SDK lists only x86 runtimes, and an x64 version of the SDK lists only x64 runtimes.

  • --list-sdks

    打印已安装的 .NET Core SDK 的列表。Prints out a list of the installed .NET Core SDKs.

  • -h|--help

    打印可用命令列表。Prints out a list of available commands.

用于运行命令的 SDK 选项SDK options for running a command

以下选项适用于使用命令的 dotnetThe following options are for dotnet with a command. 例如 dotnet build --helpFor example, dotnet build --help.

  • -d|--diagnostics

    启用诊断输出。Enables diagnostic output.

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

  • -h|--help

    打印出给定命令的文档,如 dotnet build --helpPrints out documentation for a given command, such as dotnet build --help.

  • command options

    每个命令定义特定于该命令的选项。Each command defines options specific to that command. 有关可用选项的列表,请参阅特定命令页。See specific command page for a list of available options.

运行时选项Runtime options

dotnet 运行应用程序时,可以使用以下选项。The following options are available when dotnet runs an application. 例如 dotnet myapp.dll --roll-forward MajorFor example, dotnet myapp.dll --roll-forward Major.

  • --additionalprobingpath <PATH>

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

  • --additional-deps <PATH>

    附加 .deps.json 文件的路径。Path to an additional .deps.json file. deps.json 文件包含依赖项、编译依赖项和用于解决程序集冲突的版本信息列表。A deps.json file contains a list of dependencies, compilation dependencies, and version information used to address assembly conflicts. 有关详细信息,请参阅 GitHub 上的运行时配置文件For more information, see Runtime Configuration Files on GitHub.

  • --depsfile <PATH_TO_DEPSFILE>

    deps.json 文件的路径。Path to the deps.json file. .deps.json 文件是一个配置文件,其中包含有关运行应用程序所需的依赖项的信息。A deps.json file is a configuration file that contains information about dependencies necessary to run the application. 此文件由 .NET Core SDK 生成。This file is generated by the .NET Core SDK.

  • --runtimeconfig

    runtimeconfig.template.json 文件的路径。Path to a runtimeconfig.json file. runtimeconfig.template.json 文件是包含运行时设置的配置文件。A runtimeconfig.json file is a configuration file that contains run-time settings. 有关详细信息,请参阅 .NET Core 运行时配置设置For more information, see .NET Core run-time configuration settings.

  • --roll-forward <SETTING> 自 .NET Core SDK 3.0 起可用 。--roll-forward <SETTING> Available starting with .NET Core SDK 3.0.

    控制将前滚操作应用于应用的方式。Controls how roll forward is applied to the app. SETTING 可以为下列值之一。The SETTING can be one of the following values. 如果未指定,则 Minor 为默认类型。If not specified, Minor is the default.

    • LatestPatch - 前滚到最高补丁版本。LatestPatch - Roll forward to the highest patch version. 这会禁用次要版本前滚。This disables minor version roll forward.
    • Minor - 如果缺少所请求的次要版本,则前滚到最低的较高次要版本。Minor - Roll forward to the lowest higher minor version, if requested minor version is missing. 如果存在所请求的次要版本,则使用 LatestPatch 策略。If the requested minor version is present, then the LatestPatch policy is used.
    • Major - 如果缺少所请求的主要版本,则前滚到最低的较高主要版本和最低的次要版本。Major - Roll forward to lowest higher major version, and lowest minor version, if requested major version is missing. 如果存在所请求的主要版本,则使用 Minor 策略。If the requested major version is present, then the Minor policy is used.
    • LatestMinor - 即使存在所请求的次要版本,仍前滚到最高次要版本。LatestMinor - Roll forward to highest minor version, even if requested minor version is present. 适用于组件托管方案。Intended for component hosting scenarios.
    • LatestMajor - 即使存在所请求的主要版本,仍前滚到最高主要版本和最高次要版本。LatestMajor - Roll forward to highest major and highest minor version, even if requested major is present. 适用于组件托管方案。Intended for component hosting scenarios.
    • Disable - 不前滚。Disable - Don't roll forward. 仅绑定到指定的版本。Only bind to specified version. 建议不要将此策略用于一般用途,因为它会禁用前滚到最新补丁的功能。This policy isn't recommended for general use because it disables the ability to roll forward to the latest patches. 该值仅建议用于测试。This value is only recommended for testing.

    Disable 外,所有设置都将使用可用的最高补丁版本。With the exception of Disable, all settings will use the highest available patch version.

    前滚行为还可以在项目文件属性、运行时配置文件属性和环境变量中进行配置。Roll forward behavior can also be configured in a project file property, a run-time configuration file property, and an environment variable. 有关详细信息,请参阅主版本运行时前滚For more information, see Major-version runtime roll forward.

  • --roll-forward-on-no-candidate-fx <N> 在 .NET Core 2.x SDK 中可用 。--roll-forward-on-no-candidate-fx <N> Available in .NET Core 2.x SDK.

    所需的共享框架不可用时,请定义行为。Defines behavior when the required shared framework is not available. N 可以是:N can be:

    • 0 - 禁用次要版本前滚。0 - Disable even minor version roll forward.
    • 1 - 前滚次要版本,但不前滚主版本。1 - Roll forward on minor version, but not on major version. 这是默认行为。This is the default behavior.
    • 2 - 前滚次要和主版本。2 - Roll forward on minor and major versions.

    有关详细信息,请参阅前滚For more information, see Roll forward.

    从 .NET Core 3.0 开始,此选项被 --roll-forward 取代,应改为使用此取代项。Starting with .NET Core 3.0, this option is superseded by --roll-forward, and that option should be used instead.

  • --fx-version <VERSION>

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

    此选项将重写应用程序 .runtimeconfig.json 文件中第一个框架引用的版本。This option overrides the version of the first framework reference in the application's .runtimeconfig.json file. 这意味着,仅当只有一个框架引用时,它才会按预期方式工作。This means it only works as expected if there's just one framework reference. 如果应用程序具有多个框架引用,则使用此选项可能会导致错误。If the application has more than one framework reference, using this option may cause errors.

dotnet 命令dotnet commands


命令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 pushdotnet nuget push 将包推送到服务器,并将其发布。Pushes a package to the server and publishes it.
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 add sourcedotnet nuget add source 添加 NuGet 源。Adds a NuGet source.
dotnet nuget disable sourcedotnet nuget disable source 禁用 NuGet 源。Disables a NuGet source.
dotnet nuget enable sourcedotnet nuget enable source 启用 NuGet 源。Enables a NuGet source.
dotnet nuget list sourcedotnet nuget list source 列出所有已配置的 NuGet 源。Lists all configured NuGet sources.
dotnet nuget remove sourcedotnet nuget remove source 删除 NuGet 源。Removes a NuGet source.
dotnet nuget update sourcedotnet nuget update source 更新 NuGet 源。Updates a NuGet source.

全局、工具路径和本地工具命令Global, tool-path, and local tools commands

工具是控制台应用程序,它们从 NuGet 包中安装并从命令提示符处进行调用。Tools are console applications that are installed from NuGet packages and are invoked from the command prompt. 你可自行编写工具,也可安装由第三方编写的工具。You can write tools yourself or install tools written by third parties. 工具也称为全局工具、工具路径工具和本地工具。Tools are also known as global tools, tool-path tools, and local tools. 有关详细信息,请参阅 .NET Core 工具概述For more information, see .NET Core tools overview. 全局和工具路径工具从 .NET Core SDK 2.1 开始可用。Global and tool-path tools are available starting with .NET Core SDK 2.1. 本地工具从 .NET Core SDK 3.0 开始可用。Local tools are available starting with .NET Core SDK 3.0.

命令Command 函数Function
dotnet tool installdotnet tool install 在计算机上安装工具。Installs a tool on your machine.
dotnet tool listdotnet tool list 列出计算机上当前安装的所有全局、工具路径或本地工具。Lists all global, tool-path, or local tools currently installed on your machine.
dotnet tool uninstalldotnet tool uninstall 从计算机中卸载工具。Uninstalls a tool from your machine.
dotnet tool updatedotnet tool update 更新计算机上安装的工具。Updates a tool that is installed 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.


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

dotnet new console

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

dotnet build

运行应用程序:Run an application:

dotnet myapp.dll

环境变量Environment variables


    指定 .NET Core 运行时的位置(如果运行时未安装在默认位置)。Specifies the location of the .NET Core runtimes, if they are not installed in the default location. Windows 上的默认位置为 C:\Program Files\dotnetThe default location on Windows is C:\Program Files\dotnet. Linux 和 macOS 上的默认位置为 /usr/share/dotnetThe default location on Linux and macOS is /usr/share/dotnet. 此环境变量仅在通过生成的可执行文件 (apphosts) 运行应用时使用。This environment variable is used only when running apps via generated executables (apphosts). 在 64 位 OS 上运行 32 位可执行文件时,改用 DOTNET_ROOT(x86)DOTNET_ROOT(x86) is used instead when running a 32-bit executable on a 64-bit OS.


    全局包文件夹。The global packages folder. 如果未设置,则默认为 Unix 上的 ~/.nuget/packages 或 Windows 上的 %userprofile%\.nuget\packagesIf not set, it defaults to ~/.nuget/packages on Unix or %userprofile%\.nuget\packages on Windows.


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


    指定是否在首次运行时显示 .NET Core 欢迎消息和遥测消息。Specifies whether .NET Core welcome and telemetry messages are displayed on first run. 设置为 true 可将这些消息静音(接受 true1yes 值),或者,设置为 false 可允许显示消息(接受 false0no 值)。Set to true to mute these messages (values true, 1, or yes accepted) or set to false to allow (values false, 0, or no accepted). 如果未设置,则默认值为 false,表示在首次运行时将显示消息。If not set, the default is false and the messages will be displayed on first run. 此标志对遥测不起作用(请参阅 DOTNET_CLI_TELEMETRY_OPTOUT 中关于如何选择不发送遥测数据的信息)。This flag has no effect on telemetry (see DOTNET_CLI_TELEMETRY_OPTOUT for opting out of sending telemetry).


    指定是否收集并向 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.


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

  • DOTNET_ROLL_FORWARD 自 .NET Core 3.x 起可用。DOTNET_ROLL_FORWARD Available starting with .NET Core 3.x.

    确定前滚行为。Determines roll forward behavior. 有关详细信息,请参阅本文章前面介绍的 --roll-forward 选项。For more information, see the --roll-forward option earlier in this article.


    如果设置为 1(已启用),则允许从发布版本前滚到预发行版本。If set to 1 (enabled), enables rolling forward to a pre-release version from a release version. 默认情况下(0 - 禁用),请求 .NET Core 运行时的发行版时,前滚仅考虑已安装的发行版本。By default (0 - disabled), when a release version of .NET Core runtime is requested, roll-forward will only consider installed release versions.

    有关详细信息,请参阅前滚For more information, see Roll forward.


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

    此设置在 .NET Core 3.0 中被 DOTNET_ROLL_FORWARD 取代。This setting is superseded in .NET Core 3.0 by DOTNET_ROLL_FORWARD. 应改为使用新设置。The new settings should be used instead.


    使用区域设置值(如 en-us)设置 CLI UI 的语言。Sets the language of the CLI UI using a locale value such as en-us. 支持的值与 Visual Studio 中的值相同。The supported values are the same as for Visual Studio. 有关详细信息,请参阅 Visual Studio 安装文档中有关更改安装程序语言一节。For more information, see the section on changing the installer language in the Visual Studio installation documentation. .NET 资源管理器规则适用,因此你无需选取精确匹配项 — 你还可以在 CultureInfo 树中选取后代。The .NET resource manager rules apply, so you don't have to pick an exact match—you can also pick descendants in the CultureInfo tree. 例如,如果将其设置为 fr-CA,CLI 将查找并使用 fr 翻译。For example, if you set it to fr-CA, the CLI will find and use the fr translations. 如果你将其设置为不受支持的语言,CLI 会退回到英语。If you set it to a language that is not supported, the CLI falls back to English.


    对于启用了 GUI 的已生成可执行文件 - 禁用对话框弹出窗口,此窗口通常显示某些类错误。For GUI-enabled generated executables - disables dialog popup, which normally shows for certain classes of errors. 在这些情况下,它仅写入到 stderr 并退出。It only writes to stderr and exits in those cases.


    等效于 CLI 选项 --additional-depsEquivalent to CLI option --additional-deps.


    替代检测到的 RID。Overrides the detected RID.


    程序集解析在某些情况下将回退到的“共享存储”的位置。Location of the "shared store" which assembly resolution falls back to in some cases.


    要从中加载和执行启动挂钩的程序集列表。List of assemblies to load and execute startup hooks from.

  • DOTNET_BUNDLE_EXTRACT_BASE_DIR 自 .NET Core 3.x 起可用。DOTNET_BUNDLE_EXTRACT_BASE_DIR Available starting with .NET Core 3.x.

    指定在执行单文件应用程序之前将其提取到的目录。Specifies a directory to which a single-file application is extracted before it is executed.

    有关详细信息,请参阅单文件可执行文件For more information, see Single-file executables.


    控制来自托管组件(例如 dotnet.exehostfxrhostpolicy)的诊断跟踪。Controls diagnostics tracing from the hosting components, such as dotnet.exe, hostfxr, and hostpolicy.

    • COREHOST_TRACE=[0/1] -默认值为 0 - 禁用跟踪。COREHOST_TRACE=[0/1] - default is 0 - tracing disabled. 如果设置为 1,则启用诊断跟踪。If set to 1, diagnostics tracing is enabled.
    • COREHOST_TRACEFILE=<file path> - 仅当通过 COREHOST_TRACE=1 启用跟踪时才会生效。COREHOST_TRACEFILE=<file path> - only has effect if tracing is enabled via COREHOST_TRACE=1. 设置后,跟踪信息将写入指定的文件,否则会将跟踪信息写入 stderrWhen set, the tracing information is written to the specified file, otherwise the tracing information is written to stderr. 自 .NET Core 3.x 起可用。Available starting with .NET Core 3.x.
    • COREHOST_TRACE_VERBOSITY=[1/2/3/4] - 默认值为 4COREHOST_TRACE_VERBOSITY=[1/2/3/4] - default is 4. 此设置仅在通过 COREHOST_TRACE=1 启用跟踪时使用。The setting is used only when tracing is enabled via COREHOST_TRACE=1. 自 .NET Core 3.x 起可用。Available starting with .NET Core 3.x.
      • 4 - 写入所有跟踪信息4 - all tracing information is written
      • 3 - 仅写入信息性、警告和错误消息3 - only informational, warning and error messages are written
      • 2 - 仅写入警告和错误消息2 - only warning and error messages are written
      • 1 - 仅写入错误消息1 - only error messages are written

    获取有关应用程序启动的详细跟踪信息的典型方法是设置 COREHOST_TRACE=1COREHOST_TRACEFILE=host_trace.txt,然后运行该应用程序。The typical way to get detailed trace information about application startup is to set COREHOST_TRACE=1 and COREHOST_TRACEFILE=host_trace.txt and then run the application. 将在当前目录中创建一个新文件 host_trace.txt,其中包含详细信息。A new file host_trace.txt will be created in the current directory with the detailed information.

请参阅See also