dotnet命令(不掌握)

安装好.Net Core SDK后就可以使用dotnet命令。

dotnet命令
命令 解释
dotnet 命令本身,可以使用dotnet app.dll运行应用
dotnet build 生成.NET Core应用程序
dotnet clean 清理项目生成的输出
dotnet new 为给定的模板初始化C#或F#项目
dotnet pack 创建代码的NuGet包
dotnet publish

发布应用程序及其依赖项到文件夹

发布.NET依赖于框架或独立应用程序

dotnet restore 还原给定应用程序的依赖项
dotnet run 从源代码运行应用程序
dotnet sln 用于添加、删除和列出解决方案文件中项目的选项
dotnet test 使用测试运行程序运行测试
dotnet help 显示指定命令更详细的在线文档
dotnet store 将指定的程序集存储到运行时包存储区

项目修改命令
命令 解释
dotnet add reference 添加项目引用
dotnet list reference 列出项目引用
dotnet remove reference 删除项目引用
dotnet add package 添加NuGet包
dotnet remove package 删除NuGet包

高级命令
命令 解释
dotnet nuget delete 从服务区删除或取消列出包
dotnet nuget locals 清除或列出本地NuGet资源,例如HTTP请求缓存、临时缓存或计算机范围的全局包文件夹
dotnet nuget push 将包推送到服务器,并将其发布
dotnet msbuild 访问功能完备的MSBuild

工具管理命令
命令 解释
dotnet tool install 安装指定.NET Core工具
dotnet tool list 列出当前计算机上的.net Core工具
dotnet tool update 更新指定.NET Core工具
dotnet tool uninstall 卸载指定.NET Core工具

1、创建(dotnet new)

首先创建一个项目,这里创建一个控制台程序

dotnet new console -o app

dotnet new的更多参数可以参考帮助dotnet new -h。

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core> dotnet new console -o app

Welcome to .NET 7.0!
---------------------
SDK Version: 7.0.100

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\app\app.csproj:
  Determining projects to restore...
  Restored C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\app\app.csproj (in 322 ms).
Restore succeeded.


PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core>

2、打包(dotnet pack)

接着创建一个classlib,也就是类库

dotnet new classlib -o applib

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core> dotnet new classlib -o applib
The template "Class Library" was created successfully.

Processing post-creation actions...
Restoring C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\applib.csproj:
  Determining projects to restore...
  Restored C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\applib.csproj (in 468 ms).
Restore succeeded.


PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core>

创建好以后,编写一些自己的代码然后进行打包,替换Class1.cs如下:

public class DotnetClass
{
    public int Id    {    get;    set;    }
    
    public int GetNewId(int i)
    {
        return Id+i;
    }
}

这里增加一个属性和方法,然后打包。

cd applib

dotnet pack

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core> cd applib
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib> dotnet pack
MSBuild version 17.4.0+18d5aef85 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  applib -> C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\bin\Debug\net7.0\applib.dll
  Successfully created package 'C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\bin\Debug\applib.1.0.0.nupkg'.
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib>

dotnet命令(不掌握)_第1张图片

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib> dotnet pack
MSBuild version 17.4.0+18d5aef85 for .NET
  Determining projects to restore...
  Restored C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\applib.csproj (in 276 ms).
  applib -> C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\bin\Debug\net7.0\applib.dll
  Successfully created package 'C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\bin\Debug\applib.1.0.0.nupkg'.
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib>

打好包以后,将applib添加到app的app.csproj中。

在app.csproj中添加如下代码:

  
    
  

添加好以后切换到app目录,进行还原。使用的命令是:dotnet restore -s包的路径

dotnet命令(不掌握)_第2张图片

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib> cd..
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core> dotnet restore -s C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\bin\Debug
MSBUILD : error MSB1001: Unknown switch.
    Full command line: 'C:\Program Files\dotnet\sdk\7.0.100\MSBuild.dll -maxcpucount -verbosity:m -nologo -target:Restore -property:RestoreSources=C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\bin\Debug -distributedlogger:Microsoft.DotNet.Tools.MSBuild.MSBuildLogger,C:\Program Files\dotnet\sdk\7.0.100\dotnet.dll*Microsoft.DotNet.Tools.MSBuild.MSBuildForwardingLogger,C:\Program Files\dotnet\sdk\7.0.100\dotnet.dll'
  Switches appended by response files:
Switch: -

For switch syntax, type "MSBuild -help"
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core>

dotnet命令(不掌握)_第3张图片

PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core> cd app
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\app> dotnet restore -s C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\bin\Debug
MSBUILD : error MSB1001: Unknown switch.
    Full command line: 'C:\Program Files\dotnet\sdk\7.0.100\MSBuild.dll -maxcpucount -verbosity:m -nologo -target:Restore -property:RestoreSources=C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\bin\Debug -distributedlogger:Microsoft.DotNet.Tools.MSBuild.MSBuildLogger,C:\Program Files\dotnet\sdk\7.0.100\dotnet.dll*Microsoft.DotNet.Tools.MSBuild.MSBuildForwardingLogger,C:\Program Files\dotnet\sdk\7.0.100\dotnet.dll'
  Switches appended by response files:
Switch: -

For switch syntax, type "MSBuild -help"
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\app>

这样就能在app项目中调用applib里面的方法和属性了。

3、运行(dotnet build及dotnet run)

4、测试(dptmet test)

5、发布(dotnet publish)

发布项目使其可以跨平台运行。

win10 64位系统:

dotnet publish -r win10-x64

Ubuntu 16.04 64位系统 :

dotnet publish -r ubuntu.16.04-x64

将ubuntu.16.04-x64\publish文件夹上传至Ubuntu系统,设置app的权限为可执行chmod 755 app,然后./app就可以运行。

dotnet publish -r指定系统ID,即可发布对应的系统 应用。

PS C:\Users\a-xiaobodou> dotnet new -all
“dotnet new”命令基于模板创建 .NET 项目。

常用模板包括:
模板名                短名称        语言        标记
--------------------  ------------  ----------  -------------------
控制台应用            console       [C#],F#,VB  Common/Console
类库                  classlib      [C#],F#,VB  Common/Library
ASP.NET Core Web App  webapp,razor  [C#]        Web/MVC/Razor Pages
Blazor Server App     blazorserver  [C#]        Web/Blazor
Windows 窗体应用      winforms      [C#],VB     Common/WinForms
WPF 应用程序          wpf           [C#],VB     Common/WPF

例如:
   dotnet new console

通过以下方式显示模板选项:
   dotnet new console -h
通过以下方式显示已安装的所有模板:
   dotnet new --list
通过以下方式显示 NuGet.org 上可用模板:
   dotnet new web --search
PS C:\Users\a-xiaobodou> 

PS C:\Users\a-xiaobodou> dotnet new --list
这些模板已匹配你的输入:

模板名                       短名称               语言        标记
---------------------------  -------------------  ----------  ---------------------------------------------------------
.NET MAUI Blazor 应用        maui-blazor          [C#]        MAUI/Android/iOS/macOS/Mac Catalyst/Windows/Tizen/Blazor
.NET MAUI ContentPage (C#)   maui-page-csharp     [C#]        MAUI/Android/iOS/macOS/Mac Catalyst/WinUI/Tizen/Xaml/Code
.NET MAUI ContentPage (X...  maui-page-xaml       [C#]        MAUI/Android/iOS/macOS/Mac Catalyst/WinUI/Tizen/Xaml/Code
.NET MAUI ContentView (C#)   maui-view-csharp     [C#]        MAUI/Android/iOS/macOS/Mac Catalyst/WinUI/Tizen/Xaml/Code
.NET MAUI ContentView (X...  maui-view-xaml       [C#]        MAUI/Android/iOS/macOS/Mac Catalyst/WinUI/Tizen/Xaml/Code
.NET MAUI ResourceDictio...  maui-dict-xaml       [C#]        MAUI/Android/iOS/macOS/Mac Catalyst/WinUI/Xaml/Code
.NET MAUI类库                mauilib              [C#]        MAUI/Android/iOS/macOS/Mac Catalyst/Windows/Tizen
.NET MAUI应用                maui                 [C#]        MAUI/Android/iOS/macOS/Mac Catalyst/Windows/Tizen
解决方案文件                 sln                              Solution
控制台应用                   console              [C#],F#,VB  Common/Console
类库                         classlib             [C#],F#,VB  Common/Library
Android Activity template    android-activity     [C#]        Android/Mobile
Android Application          android              [C#]        Android/Mobile
Android Class Library        androidlib           [C#]        Android/Mobile
Android Java Library Bin...  android-bindinglib   [C#]        Android/Mobile
Android Layout template      android-layout       [C#]        Android/Mobile
Android Wear Application     androidwear          [C#]        Android/Mobile
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 (Mo...  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
ASP.NET Core with React....  reactredux           [C#]        Web/MVC/SPA
Blazor Server App            blazorserver         [C#]        Web/Blazor
Blazor WebAssembly App       blazorwasm           [C#]        Web/Blazor/WebAssembly/PWA
Dotnet 本地工具清单文件      tool-manifest                    Config
dotnet gitignore 文件        gitignore                        Config
EditorConfig 文件            editorconfig                     Config
global.json file             globaljson                       Config
iOS 绑定库(预览)             iosbinding           [C#]        iOS/Mobile
iOS 控制器模板(预览版)       ios-controller       [C#]        iOS/Mobile
iOS 类库(预览)               ioslib               [C#]        iOS/Mobile
iOS 选项卡式应用程序(预...   ios-tabbed           [C#]        iOS/Mobile
iOS 应用程序(预览)           ios                  [C#]        iOS/Mobile
MacCatalyst 绑定库(预览)     maccatalystbinding   [C#]        macOS/Mac Catalyst
MacCatalyst 应用程序(预览)   maccatalyst          [C#]        macOS/Mac Catalyst
MSTest Test Project          mstest               [C#],F#,VB  Test/MSTest
MVC ViewImports              viewimports          [C#]        Web/ASP.NET
MVC ViewStart                viewstart            [C#]        Web/ASP.NET
NuGet 配置                   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 Class Library
Razor Component              razorcomponent       [C#]        Web/ASP.NET
Razor Page                   page                 [C#]        Web/ASP.NET
Web 配置                     webconfig                        Config
Windows 窗体控件库           winformscontrollib   [C#],VB     Common/WinForms
Windows 窗体类库             winformslib          [C#],VB     Common/WinForms
Windows 窗体应用             winforms             [C#],VB     Common/WinForms
Worker Service               worker               [C#],F#     Common/Worker/Web
WPF 应用程序                 wpf                  [C#],VB     Common/WPF
WPF Class library            wpflib               [C#],VB     Common/WPF
WPF Custom Control Library   wpfcustomcontrollib  [C#],VB     Common/WPF
WPF User Control Library     wpfusercontrollib    [C#],VB     Common/WPF
xUnit Test Project           xunit                [C#],F#,VB  Test/xUnit

PS C:\Users\a-xiaobodou>

PS C:\Users\a-xiaobodou> dotnet new --help
Description:
  Template Instantiation Commands for .NET CLI.

Usage:
  dotnet new [ [...]] [options]
  dotnet new [command] [options]

Arguments:
    A short name of the template to create.
          Template specific options to use.

Options:
  -o, --output     Location to place the generated output.
  -n, --name         The name for the output being created. If no name is specified, the name of the output
                           directory is used.
  --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.
  --no-update-check        Disables checking for the template package updates when instantiating a template.
  --project       The project that should be used for context evaluation.
  -v, --verbosity   Sets the verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], and diag[nostic].
                           [default: normal]
  -d, --diagnostics        Enables diagnostic output.
  -?, -h, --help           Show command line help.

Commands:
  create    Instantiates a template with given short name. An alias of 'dotnet new