November 30, 2023
By: Kevin
dotnet命令全面介绍
介绍
是一个全功能的.net程序管理工具, 负责项目,解决方案的依赖管理, 编译, 执行, 打包发布等全部工作.
可以类比与Java的Maven和JS的NPM. 当然dotnet更强大一些.
首先看一下自带的帮助:
dotnet --help
使用情况: dotnet [runtime-options] [path-to-application] [arguments]
执行 .NET 应用程序。
runtime-options:
--additionalprobingpath <path> 要探测的包含探测策略和程序集的路径。
--additional-deps <path> 指向其他 deps.json 文件的路径。
--depsfile 指向 <application>.deps.json 文件的路径。
--fx-version <version> 要用于运行应用程序的安装版共享框架的版本。
--roll-forward <setting> 前滚至框架版本(LatestPatch, Minor, LatestMinor, Major, LatestMajor, Disable)。
--runtimeconfig 指向 <application>.runtimeconfig.json 文件的路径。
path-to-application:
要执行的应用程序 .dll 文件的路径。
使用情况: dotnet [sdk-options] [command] [command-options] [arguments]
执行 .NET SDK 命令。
sdk-options:
-d|--diagnostics 启用诊断输出。
-h|--help 显示命令行帮助。
--info 显示 .NET 信息。
--list-runtimes 显示安装的运行时。
--list-sdks 显示安装的 SDK。
--version 显示使用中的 .NET SDK 版本。
SDK 命令:
add 将包或引用添加到 .NET 项目。
build 生成 .NET 项目。
build-server 与由生成版本启动的服务器进行交互。
clean 清理 .NET 项目的生成输出。
format 将样式首选项应用到项目或解决方案。
help 显示命令行帮助。
list 列出 .NET 项目的项目引用。
msbuild 运行 Microsoft 生成引擎(MSBuild)命令。
new 创建新的 .NET 项目或文件。
nuget 提供其他 NuGet 命令。
pack 创建 NuGet 包。
publish 发布 .NET 项目进行部署。
remove 从 .NET 项目中删除包或引用。
restore 还原 .NET 项目中指定的依赖项。
run 生成并运行 .NET 项目输出。
sdk 管理 .NET SDK 安装。
sln 修改 Visual Studio 解决方案文件。
store 在运行时包存储中存储指定的程序集。
test 使用 .NET 项目中指定的测试运行程序运行单元测试。
tool 安装或管理扩展 .NET 体验的工具。
vstest 运行 Microsoft 测试引擎(VSTest)命令。
workload 管理可选工作负荷。
捆绑工具中的其他命令:
dev-certs 创建和管理开发证书。
fsi 启动 F# 交互/执行 F# 脚本。
sql-cache SQL Server 缓存命令行工具。
user-secrets 管理开发用户密码。
watch 启动文件观察程序,它会在文件发生更改时运行命令。
运行 "dotnet [command] --help",获取有关命令的详细信息。
项目模板
可以用dotent来创建各类项目/解决方案模板, 用于创建不同类型的项目/解决方案.
查看所有的项目模板
dotnet new --listThese templates matched your input: Template Name Short Name Language Tags -------------------------------------------- -------------- ---------- -------------------------- ASP.NET Core Empty web [C#],F# Web/Empty ASP.NET Core gRPC Service grpc [C#] Web/gRPC ASP.NET Core Web API webapi [C#],F# Web/WebAPI ASP.NET Core Web App webapp,razor [C#] Web/MVC/Razor Pages ASP.NET Core Web App (Model-View-Controller) mvc [C#],F# Web/MVC ASP.NET Core with Angular angular [C#] Web/MVC/SPA ASP.NET Core with React.js react [C#] Web/MVC/SPA Blazor Server App blazorserver [C#] Web/Blazor Blazor WebAssembly App blazorwasm [C#] Web/Blazor/WebAssembly/PWA Class Library classlib [C#],F#,VB Common/Library Console App console [C#],F#,VB Common/Console dotnet gitignore file gitignore Config Dotnet local tool manifest file tool-manifest Config EditorConfig file editorconfig Config global.json file globaljson Config MSTest Test Project mstest [C#],F#,VB Test/MSTest MVC ViewImports viewimports [C#] Web/ASP.NET MVC ViewStart viewstart [C#] Web/ASP.NET NuGet Config nugetconfig Config NUnit 3 Test Item nunit-test [C#],F#,VB Test/NUnit NUnit 3 Test Project nunit [C#],F#,VB Test/NUnit Protocol Buffer File proto Web/gRPC Razor Class Library razorclasslib [C#] Web/Razor/Library Razor Component razorcomponent [C#] Web/ASP.NET Razor Page page [C#] Web/ASP.NET Solution File sln,solution Solution Web Config webconfig Config Worker Service worker [C#],F# Common/Worker/Web xUnit Test Project xunit [C#],F#,VB Test/xUnit新建一个solution(sln)
以下的脚本会创建一个DemoSolution目录.并在目录下放置一个和目录同名的sln文件.
dotnet new sln -o DemoSolution已成功创建模板“解决方案文件”。控制台程序
新建的程序放到特定目录
可以通过
-f net7.0来指定使用的frameworkdotnet new console -o DemoSolution/DemoConsole --force执行这个控制台程序
dotnet runHello World!
创建一个类库
dotnet new classlib -o LibName
关联解决方案和project
把一个项目关联到解决方案当中.
dotnet sln add DemoConsole.csproj已将项目“DemoConsole\DemoConsole.csproj”添加到解决方案中。webapi
dotnet new webapi -f net6.0 -o WebAPI
clojureclr项目
lein new lein-clr foo搜索可以使用的nuget包
dotnet nuget list sqliteSpecify --help for a list of available options and commands. error: Unrecognized command or argument 'sqlite' Usage: dotnet nuget list [options] [command] Options: -h|--help Show help information Commands: client-cert 列出配置中的所有客户端证书。 source 列出所有配置的 NuGet 源。 Use "list [command] --help" for more information about a command.为DemoConsole应用增加一个依赖库
dotnet add package Dapper正在确定要还原的项目… Writing C:\Users\li_zh\AppData\Local\Temp\tmp4A15.tmp info : 正在将包“Dapper”的 PackageReference 添加到项目“C:\Users\li_zh\AppData\Roaming\sandbox\rc\mechanical-testing\DemoSolution\DemoConsole\DemoConsole.csproj”。 info : GET https://api.nuget.org/v3/registration5-gz-semver2/dapper/index.json info : OK https://api.nuget.org/v3/registration5-gz-semver2/dapper/index.json 1757 毫秒 info : 正在还原 C:\Users\li_zh\AppData\Roaming\sandbox\rc\mechanical-testing\DemoSolution\DemoConsole\DemoConsole.csproj 的包... info : GET https://api.nuget.org/v3-flatcontainer/dapper/index.json info : OK https://api.nuget.org/v3-flatcontainer/dapper/index.json 1347 毫秒 info : GET https://api.nuget.org/v3-flatcontainer/dapper/2.0.123/dapper.2.0.123.nupkg info : OK https://api.nuget.org/v3-flatcontainer/dapper/2.0.123/dapper.2.0.123.nupkg 328 毫秒 info : 已通过内容哈希 RDFF4rBLLmbpi6pwkY7q/M6UXHRJEOerplDGE5jwEkP/JGJnBauAClYavNKJPW1yOTWRPIyfj4is3EaJxQXILQ== 从 https://api.nuget.org/v3/index.json 安装 Dapper 2.0.123 。 info : 包“Dapper”与项目“C:\Users\li_zh\AppData\Roaming\sandbox\rc\mechanical-testing\DemoSolution\DemoConsole\DemoConsole.csproj”中指定的所有框架均兼容。 info : 包“Dapper”(版本为 2.0.123)的 PackageReference 已添加到文件“C:\Users\li_zh\AppData\Roaming\sandbox\rc\mechanical-testing\DemoSolution\DemoConsole\DemoConsole.csproj”。 info : 将资产文件写入磁盘。路径: C:\Users\li_zh\AppData\Roaming\sandbox\rc\mechanical-testing\DemoSolution\DemoConsole\obj\project.assets.json log : 已还原 C:\Users\li_zh\AppData\Roaming\sandbox\rc\mechanical-testing\DemoSolution\DemoConsole\DemoConsole.csproj (用时 2.67 sec)。项目中的告警控制
配置在PropertyGroup中, 可以屏蔽不希望关注的告警.
<PropertyGroup> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <WarningsAsErrors></WarningsAsErrors> <NoWarn>$(NoWarn);CS8600;CS8601;CS8602;CS8603;CS8604;CS8625;CS8618</NoWarn> </PropertyGroup>
所在目录
全局目录
| OS | Path |
|---|---|
| Linux/macOS | $HOME/.dotnet/tools |
| Windows | %USERPROFILE%\dotnet\tools{=latex} |
版本管理
当前版本
dotnet --version
列出全部版本
dotnet --list-sdks
: 6.0.401
dotnet使用global.json作为配置
MS的官方文档 在没有配置的情况下, 只会使用最新的版本 简单来说, 使用命令
全局设置
在系统根目录下执行 dotnet new globaljson –sdk-version 6.0.401 生成文件
{ "sdk": { "version": "6.0.401" } }特定工程(文件夹)设置
在特定目录下执行相应命令
dotnet tool
安装特定工具
dotnet tool install -h
Description:
Install global or local tool. Local tools are added to manifest and restored.
Usage:
dotnet tool install [<PACKAGE_ID>] [options]
Arguments:
<PACKAGE_ID> The NuGet Package Id of the tool to install.
Options:
-g, --global Install the tool for the current user.
--local Install the tool and add to the local tool manifest (default).
--tool-path <PATH> The directory where the tool will be installed. The directory will be created if it does not exist.
--version <VERSION> The version of the tool package to install.
--configfile <FILE> The NuGet configuration file to use.
--tool-manifest <PATH> Path to the manifest file.
--add-source <SOURCE> Add an additional NuGet package source to use during installation.
--framework <FRAMEWORK> The target framework to install the tool for.
--prerelease determining whether to include pre-release packages.
--disable-parallel Prevent restoring multiple projects in parallel.
--ignore-failed-sources Treat package source failures as warnings.
--no-cache Do not cache packages and http requests.
--interactive Allows the command to stop and wait for user input or action (for example to complete authentication).
-v, --verbosity <LEVEL> Set the MSBuild verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].
-a, --arch <arch> The target architecture.
-?, -h, --help Show command line help.
列出已经安装的工具
dotnet tool list -g
ackage Id Version Commands
-------------------------------------------------
clojure.main 1.11.0 Clojure.Main
csharprepl 0.5.1 csharprepl
dotnet-repl 0.1.182 dotnet-repl
dotnet-script 1.3.1 dotnet-script
dotnet-try 1.0.19553.4 dotnet-try
x 6.0.12 x
dotnet script
安装
dotnet tool install dotnet-script -g
升级
dotnet tool update dotnet-script -g
使用独立的库
默认是全局通用的库
AssemblyLoadContext.CurrentContextualReflectionContext 是nil
#r "nuget:Newtonsoft.Json, 10.0.1" using Newtonsoft.Json; using System.Runtime.Loader; var version = typeof(JsonConvert).Assembly.GetName().Version; Console.WriteLine(AssemblyLoadContext.CurrentContextualReflectionContext); Console.WriteLine(version);用自己独立的context
#r "nuget:Newtonsoft.Json, 10.0.1" using Newtonsoft.Json; using System.Runtime.Loader; var version = typeof(JsonConvert).Assembly.GetName().Version; Console.WriteLine(AssemblyLoadContext.CurrentContextualReflectionContext); Console.WriteLine(version);
Repl 命令
| Command | Description |
|---|---|
| #load | Load a script into the REPL (same as #load usage in CSX) |
| #r | Load an assembly into the REPL (same as #r usage in CSX) |
| #reset | Reset the REPL back to initial state (without restarting it) |
| #cls | Clear the console screen without resetting the REPL state |
| #exit | Exits the REPL |