[C#]dotnet常用命令

enviroment

dotnet --version

$ dotnet --version

6.0.401

dotnet --info

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.401
 Commit:    0906eae6f8

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  22.04
 OS Platform: Linux
 RID:         ubuntu.22.04-x64
 Base Path:   /opt/sdk/dotnet/sdk/6.0.401/

global.json file:
  Not found

Host:
  Version:      6.0.9
  Architecture: x64
  Commit:       163a63591c

.NET SDKs installed:
  6.0.401 [/opt/sdk/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.9 [/opt/sdk/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.9 [/opt/sdk/dotnet/shared/Microsoft.NETCore.App]

Download .NET:
  https://aka.ms/dotnet-download

Learn about .NET Runtimes and SDKs:
  https://aka.ms/dotnet/runtimes-sdk-info

dotnet --list-sdks

$ dotnet --list-sdks

6.0.401 [/opt/sdk/dotnet/sdk]

dotnet --list-runtimes

$ dotnet --list-runtimes

Microsoft.AspNetCore.App 6.0.9 [/opt/sdk/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.9 [/opt/sdk/dotnet/shared/Microsoft.NETCore.App]

dotnet sdk check

检查 SDK 和 Runtime 版本,是否有更新

$ dotnet sdk check

.NET SDKs:
Version      Status     
------------------------
6.0.401      Up to date.

Try out the newest .NET SDK features with .NET 7.0.100-rc.1.22431.12.

.NET Runtimes:
Name                          Version      Status     
------------------------------------------------------
Microsoft.AspNetCore.App      6.0.9        Up to date.
Microsoft.NETCore.App         6.0.9        Up to date.

new

dotnet new -l

$ dotnet new -l

These 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                


按编程语言过滤

$ dotnet new -l -lang C#
These templates matched your input: language='C#'

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            
MSTest Test Project                           mstest          C#,F#,VB  Test/MSTest               
MVC ViewImports                               viewimports     C#        Web/ASP.NET               
MVC ViewStart                                 viewstart       C#        Web/ASP.NET               
NUnit 3 Test Item                             nunit-test      C#,F#,VB  Test/NUnit                
NUnit 3 Test Project                          nunit           C#,F#,VB  Test/NUnit                
Razor Class Library                           razorclasslib   C#        Web/Razor/Library         
Razor Component                               razorcomponent  C#        Web/ASP.NET               
Razor Page                                    page            C#        Web/ASP.NET               
Worker Service                                worker          C#,F#     Common/Worker/Web         
xUnit Test Project                            xunit           C#,F#,VB  Test/xUnit                


按语言过滤,并显示所有列信息

$ dotnet new -l -lang F# --columns-all
These templates matched your input: language='F#'

Template Name                                 Short Name  Language  Type     Author                                            Tags             
--------------------------------------------  ----------  --------  -------  ------------------------------------------------  -----------------
ASP.NET Core Empty                            web         C#,F#     project  Microsoft                                         Web/Empty        
ASP.NET Core Web API                          webapi      C#,F#     project  Microsoft                                         Web/WebAPI       
ASP.NET Core Web App (Model-View-Controller)  mvc         C#,F#     project  Microsoft                                         Web/MVC          
Class Library                                 classlib    C#,F#,VB  project  Microsoft                                         Common/Library   
Console App                                   console     C#,F#,VB  project  Microsoft                                         Common/Console   
MSTest Test Project                           mstest      C#,F#,VB  project  Microsoft                                         Test/MSTest      
NUnit 3 Test Item                             nunit-test  C#,F#,VB  item     Aleksei Kharlov aka halex2005 (codeofclimber.ru)  Test/NUnit       
NUnit 3 Test Project                          nunit       C#,F#,VB  project  Aleksei Kharlov aka halex2005 (codeofclimber.ru)  Test/NUnit       
Worker Service                                worker      C#,F#     project  Microsoft                                         Common/Worker/Web
xUnit Test Project                            xunit       C#,F#,VB  project  Microsoft                                         Test/xUnit       


package

dotnet add package [-v ]

$ dotnet add package  Newtonsoft.Json -v 12.01

  Determining projects to restore...
  Writing /tmp/tmpkaRNrJ.tmp
info : X.509 certificate chain validation will use the fallback certificate bundle at '/opt/sdk/dotnet/sdk/6.0.401/trustedroots/codesignctl.pem'.
info : Adding PackageReference for package 'Newtonsoft.Json' into project '/opt/projects/salt_shop/identity/identity.csproj'.
info : Restoring packages for /opt/projects/salt_shop/identity/identity.csproj...
info :   GET https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json 958ms
info :   GET https://api.nuget.org/v3-flatcontainer/newtonsoft.json/13.0.1/newtonsoft.json.13.0.1.nupkg
info :   OK https://api.nuget.org/v3-flatcontainer/newtonsoft.json/13.0.1/newtonsoft.json.13.0.1.nupkg 139ms
info : Installed Newtonsoft.Json 13.0.1 from https://api.nuget.org/v3/index.json with content hash ppPFpBcvxdsfUonNcvITKqLl3bqxWbDCZIzDWHzjpdAHRFfZe0Dw9HmA0+za13IdyrgJwpkDTDA9fHaxOrt20A==.
warn : NU1603: identity depends on Newtonsoft.Json (>= 12.1.0) but Newtonsoft.Json 12.1.0 was not found. An approximate best match of Newtonsoft.Json 13.0.1 was resolved.
info : Package 'Newtonsoft.Json' is compatible with all the specified frameworks in project '/opt/projects/salt_shop/identity/identity.csproj'.
info : PackageReference for package 'Newtonsoft.Json' version '12.01' added to file '/opt/projects/salt_shop/identity/identity.csproj'.
info : Writing assets file to disk. Path: /opt/projects/salt_shop/identity/obj/project.assets.json
log  : Restored /opt/projects/salt_shop/identity/identity.csproj (in 1.66 sec).

dotnet add reference


dotnet list package/reference

  • list package
$ dotnet list package

Project 'identity' has the following package references
   [net6.0]: 
   Top-level Package             Requested   Resolved
   > Swashbuckle.AspNetCore      6.2.3       6.2.3   

  • list reference
$ dotnet list reference

There are no Project to Project references in project /opt/projects/salt_shop/identity/.

dotnet remove package [name]

$ dotnet remove package JSON.NET

info : Removing PackageReference for package 'JSON.NET' from project '/opt/projects/salt_shop/identity/identity.csproj'.

dotnet pack

将代码打包到 NuGet 包。Web 项目不可打包。默认情况下,dotnet pack 先构建项目。 如果希望避免此行为,则传递 --no-build 选项。此选项在持续集成 (CI) 生成方案中通常非常有用,你可以知道代码是之前生成的。

  • 打包当前路径下的项目 .csproj
dotnet pack
dotnet pack --output nupkgs
dotnet pack --output nupkgs  --no-build
dotnet pack --output nupkgs  --no-build --runtime win10-x64
dotnet pack --output nupkgs  --no-build --runtime win10-x64 -p:PackageVersion=2.1.0
dotnet pack --output nupkgs  --no-build --runtime win10-x64 -p:PackageVersion=2.1.0 -p:TargetFrameworks=net45

** TargetFramework 的常用值:net45, net48, net6.0, net5.0, netstandard2.1, netcoreapp3.1
全部值请查看 目标框架参考 NuGet | Microsoft Learn

  • 打包指定路径的项目
dotnet pack ~/projects/app1/project.csproj
  • 用 .nuspec 文件打包
dotnet pack ~/projects/app1/project.csproj -p:NuspecFile=~/projects/app1/project.nuspec -p:NuspecBasePath=~/projects/app1/nuget

sln / solution

dotnet new sln --name

创建 解决方案

$ dotnet new sln --name salt_shop
The template "Solution File" was created successfully.

dotnet sln add

将项目添加到解决方案

$ dotnet sln add ./identity/
Project `identity/identity.csproj` added to the solution.

$ dotnet sln add ./user/
Project `user/user.csproj` added to the solution.

dotnet sln list

查看解决方案包含的项目列表

$ dotnet sln list
Project(s)
----------
identity/identity.csproj
user/user.csproj

development

dotnet restore

$ dotnet restore

  Determining projects to restore...
/opt/projects/salt_shop/identity/identity.csproj : warning NU1603: identity depends on Newtonsoft.Json (>= 12.1.0) but Newtonsoft.Json 12.1.0 was not found. An approximate best match of Newtonsoft.Json 13.0.1 was resolved. [/opt/projects/salt_shop/salt_shop.sln]
  All projects are up-to-date for restore.

dotnet run

$ dotnet run --project ./user/user.csproj 

Building...
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7016
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5236
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /opt/projects/salt_shop/user/


dotnet publish

$ dotnet publish

MSBuild version 17.3.1+2badb37d1 for .NET
  Determining projects to restore...
/opt/projects/salt_shop/identity/identity.csproj : warning NU1603: identity depends on Newtonsoft.Json (>= 12.1.0) but Newtonsoft.Json 12.1.0 was not found. An approximate best match of Newtonsoft.Json 13.0.1 was resolved. [/opt/projects/salt_shop/salt_shop.sln]
  All projects are up-to-date for restore.
/opt/projects/salt_shop/identity/identity.csproj : warning NU1603: identity depends on Newtonsoft.Json (>= 12.1.0) but Newtonsoft.Json 12.1.0 was not found. An approximate best match of Newtonsoft.Json 13.0.1 was resolved.
/opt/projects/salt_shop/user/Controllers/UserController.cs(9,16): warning CS8603: Possible null reference return. [/opt/projects/salt_shop/user/user.csproj]
/opt/projects/salt_shop/user/Controllers/UserController.cs(14,16): warning CS8603: Possible null reference return. [/opt/projects/salt_shop/user/user.csproj]
/opt/projects/salt_shop/user/Controllers/UserController.cs(19,16): warning CS8603: Possible null reference return. [/opt/projects/salt_shop/user/user.csproj]
  goods -> /opt/projects/salt_shop/goods/bin/Debug/net6.0/goods.dll
  goods -> /opt/projects/salt_shop/goods/bin/Debug/net6.0/publish/
  identity -> /opt/projects/salt_shop/identity/bin/Debug/net6.0/identity.dll
  identity -> /opt/projects/salt_shop/identity/bin/Debug/net6.0/publish/
  user -> /opt/projects/salt_shop/user/bin/Debug/net6.0/user.dll
  user -> /opt/projects/salt_shop/user/bin/Debug/net6.0/publish/


$ dotnet publish  ./user/user.csproj 

MSBuild version 17.3.1+2badb37d1 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  user -> /opt/projects/salt_shop/user/bin/Debug/net6.0/user.dll
  user -> /opt/projects/salt_shop/user/bin/Debug/net6.0/publish/

dotnet test

$ dotnet test

Determining projects to restore...
  All projects are up-to-date for restore.

dotnet clean

$ dotnet clean

MSBuild version 17.3.1+2badb37d1 for .NET
Build started 9/23/2022 10:15:07 AM.
     1>Project "/opt/projects/salt_shop/salt_shop.sln" on node 1 (Clean target(s)).
     1>ValidateSolutionConfiguration:
         Building solution configuration "Debug|Any CPU".
     1>Project "/opt/projects/salt_shop/salt_shop.sln" (1) is building "/opt/projects/salt_shop/goods/goods.csproj" (2) on node 1 (Clean target(s)).
     2>CoreClean:
         Creating directory "obj/Debug/net6.0/".
     2>Done Building Project "/opt/projects/salt_shop/goods/goods.csproj" (Clean target(s)).
     1>Project "/opt/projects/salt_shop/salt_shop.sln" (1) is building "/opt/projects/salt_shop/identity/identity.csproj" (3) on node 2 (Clean target(s)).
     3>CoreClean:
         Deleting file "/opt/projects/salt_shop/identity/bin/Debug/net6.0/appsettings.Development.json".
         Deleting file "/opt/projects/salt_shop/identity/bin/Debug/net6.0/appsettings.json".
         Deleting file "/opt/projects/salt_shop/identity/bin/Debug/net6.0/identity".
         Deleting file "/opt/projects/salt_shop/identity/bin/Debug/net6.0/identity.deps.json".
         Deleting file "/opt/projects/salt_shop/identity/bin/Debug/net6.0/identity.runtimeconfig.json".
         Deleting file "/opt/projects/salt_shop/identity/bin/Debug/net6.0/identity.dll".
         Deleting file "/opt/projects/salt_shop/identity/bin/Debug/net6.0/identity.pdb".
         Deleting file "/opt/projects/salt_shop/identity/bin/Debug/net6.0/Microsoft.OpenApi.dll".
         Deleting file "/opt/projects/salt_shop/identity/bin/Debug/net6.0/Swashbuckle.AspNetCore.Swagger.dll".
         Deleting file "/opt/projects/salt_shop/identity/bin/Debug/net6.0/Swashbuckle.AspNetCore.SwaggerGen.dll".
         Deleting file "/opt/projects/salt_shop/identity/bin/Debug/net6.0/Swashbuckle.AspNetCore.SwaggerUI.dll".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/identity.csproj.AssemblyReference.cache".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/identity.GeneratedMSBuildEditorConfig.editorconfig".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/identity.AssemblyInfoInputs.cache".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/identity.AssemblyInfo.cs".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/identity.csproj.CoreCompileInputs.cache".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/identity.MvcApplicationPartsAssemblyInfo.cs".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/identity.MvcApplicationPartsAssemblyInfo.cache".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/staticwebassets.build.json".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/identity.csproj.CopyComplete".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/identity.dll".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/refint/identity.dll".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/identity.pdb".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/identity.genruntimeconfig.cache".
         Deleting file "/opt/projects/salt_shop/identity/obj/Debug/net6.0/ref/identity.dll".
     1>Project "/opt/projects/salt_shop/salt_shop.sln" (1) is building "/opt/projects/salt_shop/user/user.csproj" (4) on node 3 (Clean target(s)).
     4>CoreClean:
         Deleting file "/opt/projects/salt_shop/user/bin/Debug/net6.0/appsettings.Development.json".
         Deleting file "/opt/projects/salt_shop/user/bin/Debug/net6.0/appsettings.json".
         Deleting file "/opt/projects/salt_shop/user/bin/Debug/net6.0/user".
         Deleting file "/opt/projects/salt_shop/user/bin/Debug/net6.0/user.deps.json".
         Deleting file "/opt/projects/salt_shop/user/bin/Debug/net6.0/user.runtimeconfig.json".
         Deleting file "/opt/projects/salt_shop/user/bin/Debug/net6.0/user.dll".
         Deleting file "/opt/projects/salt_shop/user/bin/Debug/net6.0/user.pdb".
         Deleting file "/opt/projects/salt_shop/user/bin/Debug/net6.0/Microsoft.OpenApi.dll".
         Deleting file "/opt/projects/salt_shop/user/bin/Debug/net6.0/Swashbuckle.AspNetCore.Swagger.dll".
         Deleting file "/opt/projects/salt_shop/user/bin/Debug/net6.0/Swashbuckle.AspNetCore.SwaggerGen.dll".
         Deleting file "/opt/projects/salt_shop/user/bin/Debug/net6.0/Swashbuckle.AspNetCore.SwaggerUI.dll".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/user.csproj.AssemblyReference.cache".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/user.GeneratedMSBuildEditorConfig.editorconfig".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/user.AssemblyInfoInputs.cache".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/user.AssemblyInfo.cs".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/user.csproj.CoreCompileInputs.cache".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/user.MvcApplicationPartsAssemblyInfo.cs".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/user.MvcApplicationPartsAssemblyInfo.cache".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/staticwebassets.build.json".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/user.csproj.CopyComplete".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/user.dll".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/refint/user.dll".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/user.pdb".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/user.genruntimeconfig.cache".
         Deleting file "/opt/projects/salt_shop/user/obj/Debug/net6.0/ref/user.dll".
     3>Done Building Project "/opt/projects/salt_shop/identity/identity.csproj" (Clean target(s)).
     4>Done Building Project "/opt/projects/salt_shop/user/user.csproj" (Clean target(s)).
     1>Done Building Project "/opt/projects/salt_shop/salt_shop.sln" (Clean target(s)).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.97

dotnet build

$ dotnet build

MSBuild version 17.3.1+2badb37d1 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  identity -> /opt/projects/salt_shop/identity/bin/Debug/net6.0/identity.dll
  goods -> /opt/projects/salt_shop/goods/bin/Debug/net6.0/goods.dll
  user -> /opt/projects/salt_shop/user/bin/Debug/net6.0/user.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.19

#DOTNET  

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FL1623863129

你的打赏是我写文章最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值