CLR是开发人员与.Net Core交互的唯一的用户界面,支持跨平台运行,在windows、Linux、mac下命令一致。可通过CLR命令行实现创建、发布项目等等功能。它是.Net Core的独立开源项目,也是核心项目。.Net Core 源码git地址。安装SDK时,会自动安装CLR。安装运行时也会安装CLR,但是只有运行时环境需要的部分功能。
可以在cmd或power shell里面运行。
在D盘新建文件夹测试,在此文件夹打开Power shell。输入命令dotnet new global.json --sdk-version 3.1.301,再输入命令dotnet --version即可查看当前文件夹使用的.Net Core的版本。此文件夹及其下面的子文件夹都会默认使用当前版本,若没有global.json文件,则向上级文件夹查找,若一直找不到,则默认使用系统最新版本。
H:\测试>dotnet new
用法: new [选项]
选项:
-h, --help Displays help for this command.
-l, --list Lists templates containing the specified name. If no name is specified, lists all templates.
-n, --name The name for the output being created. If no name is specified, the name of the current directory is used.
-o, --output Location to place the generated output.
-i, --install Installs a source or a template pack.
-u, --uninstall Uninstalls a source or a template pack.
--nuget-source Specifies a NuGet source to use during install.
--type Filters templates based on available types. Predefined values are "project", "item" or "other".
--dry-run Displays a summary of what would happen if the given command line were run if it would result in a template creation.
--force Forces content to be generated even if it would change existing files.
-lang, --language Filters templates based on language and specifies the language of the template to create.
--update-check Check the currently installed template packs for updates.
--update-apply Check the currently installed template packs for update, and install the updates.
Templates Short Name Language Tags
----------------------------------------------------------------------------------------------------------------------------------
Console Application console [C#], F#, VB Common/Console
Class library classlib [C#], F#, VB Common/Library
WPF Application wpf [C#] Common/WPF
WPF Class library wpflib [C#] Common/WPF
WPF Custom Control Library wpfcustomcontrollib [C#] Common/WPF
WPF User Control Library wpfusercontrollib [C#] Common/WPF
Windows Forms (WinForms) Application winforms [C#] Common/WinForms
Windows Forms (WinForms) Class library winformslib [C#] Common/WinForms
Worker Service worker [C#] Common/Worker/Web
Unit Test Project mstest [C#], F#, VB Test/MSTest
NUnit 3 Test Project nunit [C#], F#, VB Test/NUnit
NUnit 3 Test Item nunit-test [C#], F#, VB Test/NUnit
xUnit Test Project xunit [C#], F#, VB Test/xUnit
Razor Component razorcomponent [C#] Web/ASP.NET
Razor Page page [C#] Web/ASP.NET
MVC ViewImports viewimports [C#] Web/ASP.NET
MVC ViewStart viewstart [C#] Web/ASP.NET
Blazor Server App blazorserver [C#] Web/Blazor
Blazor WebAssembly App blazorwasm [C#] Web/Blazor/WebAssembly
ASP.NET Core Empty web [C#], F# Web/Empty
ASP.NET Core Web App (Model-View-Controller) mvc [C#], F# Web/MVC
ASP.NET Core Web App webapp [C#] Web/MVC/Razor Pages
ASP.NET Core with Angular angular [C#] Web/MVC/SPA
ASP.NET Core with React.js react [C#] Web/MVC/SPA
ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPA
Razor Class Library razorclasslib [C#] Web/Razor/Library/Razor Class Library
ASP.NET Core Web API webapi [C#], F# Web/WebAPI
ASP.NET Core gRPC Service grpc [C#] Web/gRPC
dotnet gitignore file gitignore Config
global.json file globaljson Config
NuGet Config nugetconfig Config
Dotnet local tool manifest file tool-manifest Config
Web Config webconfig Config
Solution File sln Solution
Protocol Buffer File proto Web/gRPC
Examples:
dotnet new mvc --auth Individual
dotnet new web
dotnet new --help
dotnet new < TEMPLATE > [-l|–list][–type]
dotnet new < TEMPLANT >
[-lang|–language] 选择语言
[-n|–name] 项目名称
[-o|–output] 输出路径
[Templant options]
[–force]
[-i|–install]
[-u|–uninstall]
[–update-apply]
[–update-cjeck]
H:\测试>dotnet new --list --type project
用法: new [选项]
选项:
-h, --help Displays help for this command.
-l, --list Lists templates containing the specified name. If no name is specified, lists all templates.
-n, --name The name for the output being created. If no name is specified, the name of the current directory is used.
-o, --output Location to place the generated output.
-i, --install Installs a source or a template pack.
-u, --uninstall Uninstalls a source or a template pack.
--nuget-source Specifies a NuGet source to use during install.
--type Filters templates based on available types. Predefined values are "project", "item" or "other".
--dry-run Displays a summary of what would happen if the given command line were run if it would result in a template creation.
--force Forces content to be generated even if it would change existing files.
-lang, --language Filters templates based on language and specifies the language of the template to create.
--update-check Check the currently installed template packs for updates.
--update-apply Check the currently installed template packs for update, and install the updates.
Templates Short Name Language Tags
----------------------------------------------------------------------------------------------------------------------------------
Console Application console [C#], F#, VB Common/Console
Class library classlib [C#], F#, VB Common/Library
WPF Application wpf [C#] Common/WPF
WPF Class library wpflib [C#] Common/WPF
WPF Custom Control Library wpfcustomcontrollib [C#] Common/WPF
WPF User Control Library wpfusercontrollib [C#] Common/WPF
Windows Forms (WinForms) Application winforms [C#] Common/WinForms
Windows Forms (WinForms) Class library winformslib [C#] Common/WinForms
Worker Service worker [C#] Common/Worker/Web
Unit Test Project mstest [C#], F#, VB Test/MSTest
NUnit 3 Test Project nunit [C#], F#, VB Test/NUnit
xUnit Test Project xunit [C#], F#, VB Test/xUnit
Blazor Server App blazorserver [C#] Web/Blazor
Blazor WebAssembly App blazorwasm [C#] Web/Blazor/WebAssembly
ASP.NET Core Empty web [C#], F# Web/Empty
ASP.NET Core Web App (Model-View-Controller) mvc [C#], F# Web/MVC
ASP.NET Core Web App webapp [C#] Web/MVC/Razor Pages
ASP.NET Core with Angular angular [C#] Web/MVC/SPA
ASP.NET Core with React.js react [C#] Web/MVC/SPA
ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPA
Razor Class Library razorclasslib [C#] Web/Razor/Library/Razor Class Library
ASP.NET Core Web API webapi [C#], F# Web/WebAPI
ASP.NET Core gRPC Service grpc [C#] Web/gRPC
Solution File
H:\测试>dotnet new --list --type item
用法: new [选项]
选项:
-h, --help Displays help for this command.
-l, --list Lists templates containing the specified name. If no name is specified, lists all templates.
-n, --name The name for the output being created. If no name is specified, the name of the current directory is used.
-o, --output Location to place the generated output.
-i, --install Installs a source or a template pack.
-u, --uninstall Uninstalls a source or a template pack.
--nuget-source Specifies a NuGet source to use during install.
--type Filters templates based on available types. Predefined values are "project", "item" or "other".
--dry-run Displays a summary of what would happen if the given command line were run if it would result in a template creation.
--force Forces content to be generated even if it would change existing files.
-lang, --language Filters templates based on language and specifies the language of the template to create.
--update-check Check the currently installed template packs for updates.
--update-apply Check the currently installed template packs for update, and install the updates.
Templates Short Name Language Tags
--------------------------------------------------------------------------------------
NUnit 3 Test Item nunit-test [C#], F#, VB Test/NUnit
Razor Component razorcomponent [C#] Web/ASP.NET
Razor Page page [C#] Web/ASP.NET
MVC ViewImports viewimports [C#] Web/ASP.NET
MVC ViewStart viewstart [C#] Web/ASP.NET
dotnet gitignore file gitignore Config
global.json file globaljson Config
NuGet Config nugetconfig Config
Dotnet local tool manifest file tool-manifest Config
Web Config webconfig Config
Solution File sln Solution
Protocol Buffer File proto Web/gRPC
H:\测试>dotnet new --list --type other
用法: new [选项]
选项:
-h, --help Displays help for this command.
-l, --list Lists templates containing the specified name. If no name is specified, lists all templates.
-n, --name The name for the output being created. If no name is specified, the name of the current directory is used.
-o, --output Location to place the generated output.
-i, --install Installs a source or a template pack.
-u, --uninstall Uninstalls a source or a template pack.
--nuget-source Specifies a NuGet source to use during install.
--type Filters templates based on available types. Predefined values are "project", "item" or "other".
--dry-run Displays a summary of what would happen if the given command line were run if it would result in a template creation.
--force Forces content to be generated even if it would change existing files.
-lang, --language Filters templates based on language and specifies the language of the template to create.
--update-check Check the currently installed template packs for updates.
--update-apply Check the currently installed template packs for update, and install the updates.
Templates Short Name Language Tags
---------------------------------------------------------
Solution File sln Solution
H:\测试>dotnet new console --name helloword
The template "Console Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on helloword\helloword.csproj...
正在确定要还原的项目…
已还原 H:\测试\helloword\helloword.csproj (用时 167 ms)。
Restore succeeded.
-n 项目名称
-o 输出路径
H:\测试>dotnet new console -n helloword -o test
The template "Console Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on test\helloword.csproj...
正在确定要还原的项目…
已还原 H:\测试\test\helloword.csproj (用时 153 ms)。
Restore succeeded
PS H:\测试> dotnet new sln -o Demo
The template "Solution File" was created successfully.
PS H:\测试> cd Demo
PS H:\测试\Demo> dotnet new classlib -o Demo.Core
The template "Class library" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on Demo.Core\Demo.Core.csproj...
正在确定要还原的项目…
已还原 H:\测试\Demo\Demo.Core\Demo.Core.csproj (用时 258 ms)。
Restore succeeded.
PS H:\测试\Demo> dotnet new console -o Demo.main
The template "Console Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on Demo.main\Demo.main.csproj...
正在确定要还原的项目…
已还原 H:\测试\Demo\Demo.main\Demo.main.csproj (用时 159 ms)。
Restore succeeded.
PS H:\测试\Demo> dotnet sln add Demo.Core Demo.Main
已将项目“Demo.Core\Demo.Core.csproj”添加到解决方案中。
已将项目“Demo.Main\Demo.main.csproj”添加到解决方案中。
PS H:\测试\Demo> dotnet sln list
项目
--
Demo.Core\Demo.Core.csproj
Demo.Main\Demo.main.csproj
PS H:\测试\Demo> cd ..
PS H:\测试> code Demo
PS H:\测试> cd Demo
PS H:\测试\Demo> dotnet sln remove Demo.Core Demo.Main
已从解决方案中移除项目“Demo.Core\Demo.Core.csproj”。
已从解决方案中移除项目“Demo.Main\Demo.main.csproj”。
PS H:\测试\Demo> dotnet sln list
未在解决方案中找到项目。
PS H:\测试\Demo> dotnet sln add **/*.csproj
PS H:\测试\Demo> dotnet add .\Demo.main\ reference .\Demo.Core\
已将引用“..\Demo.Core\Demo.Core.csproj”添加到项目。
PS H:\测试\Demo> dotnet list .\Demo.Main\ reference
项目引用
----
..\Demo.Core\Demo.Core.csproj
PS H:\测试\Demo> dotnet add .\Demo.Core\ package NLog
正在确定要还原的项目…
Writing C:\Users\home\AppData\Local\Temp\tmp53E7.tmp
info : 正在将包“NLog”的 PackageReference 添加到项目“H:\测试\Demo\Demo.Core\Demo.Core.csproj”。
info : 正在还原 H:\测试\Demo\Demo.Core\Demo.Core.csproj 的包...
info : GET https://api.nuget.org/v3-flatcontainer/nlog/index.json
info : OK https://api.nuget.org/v3-flatcontainer/nlog/index.json 944 毫秒
info : GET https://api.nuget.org/v3-flatcontainer/nlog/4.7.4/nlog.4.7.4.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/nlog/4.7.4/nlog.4.7.4.nupkg 787 毫秒
info : 正在安装 NLog 4.7.4。
info : 包“NLog”与项目“H:\测试\Demo\Demo.Core\Demo.Core.csproj”中指定的所有框架均兼容。
info : 包“NLog”(版本为 4.7.4)的 PackageReference 已添加到文件“H:\测试\Demo\Demo.Core\Demo.Core.csproj”。
info : 正在提交还原...
info : 将资产文件写入磁盘。路径: H:\测试\Demo\Demo.Core\obj\project.assets.json
log : 已还原 H:\测试\Demo\Demo.Core\Demo.Core.csproj (用时 5.91 sec)。
PS H:\测试\Demo> dotnet list .\Demo.Core\ package
项目“Demo.Core”具有以下包引用
[netstandard2.0]:
顶级包 已请求 已解决
> NETStandard.Library (A) [2.0.3, ) 2.0.3
> NLog 4.7.4 4.7.4
(A): 自动引用的包。
PS H:\测试\Demo> dotnet remove .\Demo.Core\ package NLog
info : 正在从项目“H:\测试\Demo\Demo.Core\Demo.Core.csproj”中删除包“NLog”的 PackageReference。
dotnet build [|]
[-o|–output] 输出目录
[-f|–framework] 以哪个目标框架生成
[-c|–configuration] Debug/Realse
[-r|–runtime] RID = [os].[version]-[arch] 如 [osx.10.11-x64]
[–no-incremental] 全量 默认增量
[–no-dependencies]
[-v|–verbosity] 基本很多命令都有这个参数,输出日志 d详细,q静默,最少
PS H:\测试\Demo> dotnet build
用于 .NET Core 的 Microsoft (R) 生成引擎版本 16.6.0+5ff7b0c9e
版权所有(C) Microsoft Corporation。保留所有权利。
正在确定要还原的项目…
已还原 H:\测试\Demo\Demo.Core\Demo.Core.csproj (用时 252 ms)。
已还原 H:\测试\Demo\Demo.Main\Demo.main.csproj (用时 252 ms)。
Demo.Core -> H:\测试\Demo\Demo.Core\bin\Debug\netstandard2.0\Demo.Core.dll
Demo.main -> H:\测试\Demo\Demo.Main\bin\Debug\netcoreapp3.1\Demo.main.dll
已成功生成。
0 个警告
0 个错误
已用时间 00:00:04.95
PS H:\测试\Demo> dotnet build --configuration Release --runtime ubuntu.18.04-x64 (构建运行在linux系统上的release版本)
用于 .NET Core 的 Microsoft (R) 生成引擎版本 16.6.0+5ff7b0c9e
版权所有(C) Microsoft Corporation。保留所有权利。
正在确定要还原的项目…
已还原 H:\测试\Demo\Demo.Core\Demo.Core.csproj (用时 271 ms)。
已还原 H:\测试\Demo\Demo.Main\Demo.main.csproj (用时 2.35 min)。
Demo.Core -> H:\测试\Demo\Demo.Core\bin\Release\netstandard2.0\Demo.Core.dll
Demo.Core -> H:\测试\Demo\Demo.Core\bin\Release\netstandard2.0\ubuntu.18.04-x64\Demo.Core.dll
Demo.main -> H:\测试\Demo\Demo.Main\bin\Release\netcoreapp3.1\ubuntu.18.04-x64\Demo.main.dll
已成功生成。
0 个警告
0 个错误
已用时间 00:02:25.27
dotnet publish [|]
[-o|–output] [-f|–framework] [-c|–configuration][-r|–runtime] [–no-incremental] [–no-dependencies] [-v|–verbosity]
[–self-contained] 独立发布,不跨平台,不需要安装运行时
[–no-self-contained] 框架依赖发布,跨平台,需要安装运行时
执行时先判断当前目录是否是上一个构建之后的改动。如果没有就直接打包,否则先Build,再打包。
PS H:\测试\Demo> dotnet publish
用于 .NET Core 的 Microsoft (R) 生成引擎版本 16.6.0+5ff7b0c9e
版权所有(C) Microsoft Corporation。保留所有权利。
正在确定要还原的项目…
已还原 H:\测试\Demo\Demo.Core\Demo.Core.csproj (用时 208 ms)。
已还原 H:\测试\Demo\Demo.Main\Demo.main.csproj (用时 208 ms)。
Demo.Core -> H:\测试\Demo\Demo.Core\bin\Debug\netstandard2.0\Demo.Core.dll
Demo.Core -> H:\测试\Demo\Demo.Core\bin\Debug\netstandard2.0\publish\
Demo.main -> H:\测试\Demo\Demo.Main\bin\Debug\netcoreapp3.1\Demo.main.dll
Demo.main -> H:\测试\Demo\Demo.Main\bin\Debug\netcoreapp3.1\publish\
PS H:\测试\Demo> dotnet publish --runtime osx.10.11-x64 --self-contained (针对mac系统发布自包含项目)
用于 .NET Core 的 Microsoft (R) 生成引擎版本 16.6.0+5ff7b0c9e
版权所有(C) Microsoft Corporation。保留所有权利。
正在确定要还原的项目…
已还原 H:\测试\Demo\Demo.Core\Demo.Core.csproj (用时 290 ms)。
已还原 H:\测试\Demo\Demo.Main\Demo.main.csproj (用时 28.51 sec)。
Demo.Core -> H:\测试\Demo\Demo.Core\bin\Debug\netstandard2.0\Demo.Core.dll
Demo.Core -> H:\测试\Demo\Demo.Core\bin\Debug\netstandard2.0\osx.10.11-x64\Demo.Core.dll
Demo.Core -> H:\测试\Demo\Demo.Core\bin\Debug\netstandard2.0\osx.10.11-x64\publish\
Demo.main -> H:\测试\Demo\Demo.Main\bin\Debug\netcoreapp3.1\osx.10.11-x64\Demo.main.dll
Demo.main -> H:\测试\Demo\Demo.Main\bin\Debug\netcoreapp3.1\osx.10.11-x64\publish\
PS H:\测试\Demo\Demo.main\bin\Debug\netcoreapp3.1> dotnet run
找不到要运行的项目。请确保 H:\测试\Demo\Demo.main\bin\Debug\netcoreapp3.1 中存在项目,或使用 --project 传递项目路径。
PS H:\测试\Demo\Demo.main\bin\Debug\netcoreapp3.1> cd ..
PS H:\测试\Demo\Demo.main\bin\Debug> dotnet run
找不到要运行的项目。请确保 H:\测试\Demo\Demo.main\bin\Debug 中存在项目,或使用 --project 传递项目路径。
PS H:\测试\Demo\Demo.main\bin\Debug> cd ..
PS H:\测试\Demo\Demo.main\bin> cd ..
PS H:\测试\Demo\Demo.main> dotnet run
Hello World!
PS H:\测试\Demo\Demo.main>
PS H:\测试\Demo> cd .\Demo.main\
PS H:\测试\Demo\Demo.main> cd .\bin\
PS H:\测试\Demo\Demo.main\bin> cd .\Debug\
PS H:\测试\Demo\Demo.main\bin\Debug> cd .\netcoreapp3.1\
PS H:\测试\Demo\Demo.main\bin\Debug\netcoreapp3.1> ls
目录: H:\测试\Demo\Demo.main\bin\Debug\netcoreapp3.1
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/8/26 21:46 osx.10.11-x64
d----- 2020/8/26 21:43 publish
-a---- 2020/8/26 21:29 3584 Demo.Core.dll
-a---- 2020/8/26 21:29 564 Demo.Core.pdb
-a---- 2020/8/26 21:43 701 Demo.main.deps.json
-a---- 2020/8/26 21:29 4608 Demo.main.dll
-a---- 2020/8/26 21:29 174592 Demo.main.exe
-a---- 2020/8/26 21:29 632 Demo.main.pdb
-a---- 2020/8/26 21:43 234 Demo.main.runtimeconfig.dev.json
-a---- 2020/8/26 21:43 154 Demo.main.runtimeconfig.json
PS H:\测试\Demo\Demo.main\bin\Debug\netcoreapp3.1> dotnet .\Demo.main.dll
Hello World!
dotnet nuget push [] 推送的服务器
[-s|–source] nuget源的url地址
[-ss|–symbol-source] 发布包的同时,发布pdb文件,便于它人调试
[-t|–timeout]
[-k|–api-key] 发布包对应的账号新建key,必须有
[-sk|–symbol-api-key] 符号服务器的key
PS H:\测试> cd Demo
PS H:\测试\Demo> dotnet pack -o nupkgs
用于 .NET Core 的 Microsoft (R) 生成引擎版本 16.6.0+5ff7b0c9e
版权所有(C) Microsoft Corporation。保留所有权利。
正在确定要还原的项目…
已还原 H:\测试\Demo\Demo.Main\Demo.main.csproj (用时 276 ms)。
1 个项目(共 2 个)是最新的,无法还原。
Demo.Core -> H:\测试\Demo\Demo.Core\bin\Debug\netstandard2.0\Demo.Core.dll
已成功创建包“H:\测试\Demo\nupkgs\Demo.Core.1.0.0.nupkg”。
Demo.main -> H:\测试\Demo\Demo.Main\bin\Debug\netcoreapp3.1\Demo.main.dll
已成功创建包“H:\测试\Demo\nupkgs\Demo.main.1.0.0.nupkg”。
PS H:\测试\Demo> dotnet nuget push .\nupkgs\Demo.Core.1.0.0.nupkg -k your key -s https://api.nuget.org/v3/index.json
正在将 Demo.Core.1.0.0.nupkg 推送到 'https://www.nuget.org/api/v2/package'...
PUT https://www.nuget.org/api/v2/package/
Forbidden https://www.nuget.org/api/v2/package/ 9249 毫秒
error: Response status code does not indicate success: 403 (The specified API key is invalid, has expired, or does not have permission to access the specified package.).
Usage: dotnet nuget push [arguments] [options]
Arguments:
[root] 指定包路径和 API 密钥,将包推送到服务器。
Options:
-h|--help Show help information
--force-english-output 使用不变的基于英语的区域性强制应用程序运行。
-s|--source <source> 要使用的包源(URL、UNC/文件夹路径或包源名称)。如果在 NuGet.Config 中指定,则默认为 DefaultPushSource。
-ss|--symbol-source <source> 要使用的符号服务器 URL。
-t|--timeout <timeout> 推送到服务器的超时值(以秒为单位)。默认为 300 秒(5 分钟)。
-k|--api-key <apiKey> 服务器的 API 密钥。
-sk|--symbol-api-key <apiKey> 符号服务器的 API 密钥。
-d|--disable-buffering 推送到 HTTP(S) 服务器时禁用缓存可减少内存使用。
-n|--no-symbols 如果存在符号包,系统不会将该符号包推送到符号服务器。
--no-service-endpoint 请勿将 "api/v2/package" 追加到源 URL。
--interactive 对于身份验证等操作,允许命令阻止并要求手动操作。
--skip-duplicate 如果包和版本已存在,则跳过它并继续推送中的下一个包(若有)。