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

    1. Windows PowerShell
    2. Copyright (C) Microsoft Corporation. All rights reserved.
    3. Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
    4. PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core> dotnet new console -o app
    5. Welcome to .NET 7.0!
    6. ---------------------
    7. SDK Version: 7.0.100
    8. Telemetry
    9. ---------
    10. 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.
    11. Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry
    12. ----------------
    13. Installed an ASP.NET Core HTTPS development certificate.
    14. To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
    15. Learn about HTTPS: https://aka.ms/dotnet-https
    16. ----------------
    17. Write your first app: https://aka.ms/dotnet-hello-world
    18. Find out what's new: https://aka.ms/dotnet-whats-new
    19. Explore documentation: https://aka.ms/dotnet-docs
    20. Report issues and find source on GitHub: https://github.com/dotnet/core
    21. Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
    22. --------------------------------------------------------------------------------------
    23. The template "Console App" was created successfully.
    24. Processing post-creation actions...
    25. Restoring C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\app\app.csproj:
    26. Determining projects to restore...
    27. Restored C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\app\app.csproj (in 322 ms).
    28. Restore succeeded.
    29. PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core>

    2、打包(dotnet pack)

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

    dotnet new classlib -o applib

    1. PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core> dotnet new classlib -o applib
    2. The template "Class Library" was created successfully.
    3. Processing post-creation actions...
    4. Restoring C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\applib.csproj:
    5. Determining projects to restore...
    6. Restored C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib\applib.csproj (in 468 ms).
    7. Restore succeeded.
    8. PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core>

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

    1. public class DotnetClass
    2. {
    3. public int Id { get; set; }
    4. public int GetNewId(int i)
    5. {
    6. return Id+i;
    7. }
    8. }

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

    1. cd applib
    2. dotnet pack

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

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

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

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

    1. "applib" Version="1.0.0" />

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

    1. PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core\applib> cd..
    2. 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
    3. MSBUILD : error MSB1001: Unknown switch.
    4. 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'
    5. Switches appended by response files:
    6. Switch: -
    7. For switch syntax, type "MSBuild -help"
    8. PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core>

    1. PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ASP.NET Core> cd app
    2. 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
    3. MSBUILD : error MSB1001: Unknown switch.
    4. 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'
    5. Switches appended by response files:
    6. Switch: -
    7. For switch syntax, type "MSBuild -help"
    8. 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,即可发布对应的系统 应用。

    1. PS C:\Users\a-xiaobodou> dotnet new -all
    2. “dotnet new”命令基于模板创建 .NET 项目。
    3. 常用模板包括:
    4. 模板名 短名称 语言 标记
    5. -------------------- ------------ ---------- -------------------
    6. 控制台应用 console [C#],F#,VB Common/Console
    7. 类库 classlib [C#],F#,VB Common/Library
    8. ASP.NET Core Web App webapp,razor [C#] Web/MVC/Razor Pages
    9. Blazor Server App blazorserver [C#] Web/Blazor
    10. Windows 窗体应用 winforms [C#],VB Common/WinForms
    11. WPF 应用程序 wpf [C#],VB Common/WPF
    12. 例如:
    13. dotnet new console
    14. 通过以下方式显示模板选项:
    15. dotnet new console -h
    16. 通过以下方式显示已安装的所有模板:
    17. dotnet new --list
    18. 通过以下方式显示 NuGet.org 上可用模板:
    19. dotnet new web --search
    20. PS C:\Users\a-xiaobodou>

    1. PS C:\Users\a-xiaobodou> dotnet new --list
    2. 这些模板已匹配你的输入:
    3. 模板名 短名称 语言 标记
    4. --------------------------- ------------------- ---------- ---------------------------------------------------------
    5. .NET MAUI Blazor 应用 maui-blazor [C#] MAUI/Android/iOS/macOS/Mac Catalyst/Windows/Tizen/Blazor
    6. .NET MAUI ContentPage (C#) maui-page-csharp [C#] MAUI/Android/iOS/macOS/Mac Catalyst/WinUI/Tizen/Xaml/Code
    7. .NET MAUI ContentPage (X... maui-page-xaml [C#] MAUI/Android/iOS/macOS/Mac Catalyst/WinUI/Tizen/Xaml/Code
    8. .NET MAUI ContentView (C#) maui-view-csharp [C#] MAUI/Android/iOS/macOS/Mac Catalyst/WinUI/Tizen/Xaml/Code
    9. .NET MAUI ContentView (X... maui-view-xaml [C#] MAUI/Android/iOS/macOS/Mac Catalyst/WinUI/Tizen/Xaml/Code
    10. .NET MAUI ResourceDictio... maui-dict-xaml [C#] MAUI/Android/iOS/macOS/Mac Catalyst/WinUI/Xaml/Code
    11. .NET MAUI类库 mauilib [C#] MAUI/Android/iOS/macOS/Mac Catalyst/Windows/Tizen
    12. .NET MAUI应用 maui [C#] MAUI/Android/iOS/macOS/Mac Catalyst/Windows/Tizen
    13. 解决方案文件 sln Solution
    14. 控制台应用 console [C#],F#,VB Common/Console
    15. 类库 classlib [C#],F#,VB Common/Library
    16. Android Activity template android-activity [C#] Android/Mobile
    17. Android Application android [C#] Android/Mobile
    18. Android Class Library androidlib [C#] Android/Mobile
    19. Android Java Library Bin... android-bindinglib [C#] Android/Mobile
    20. Android Layout template android-layout [C#] Android/Mobile
    21. Android Wear Application androidwear [C#] Android/Mobile
    22. ASP.NET Core Empty web [C#],F# Web/Empty
    23. ASP.NET Core gRPC Service grpc [C#] Web/gRPC
    24. ASP.NET Core Web API webapi [C#],F# Web/WebAPI
    25. ASP.NET Core Web App webapp,razor [C#] Web/MVC/Razor Pages
    26. ASP.NET Core Web App (Mo... mvc [C#],F# Web/MVC
    27. ASP.NET Core with Angular angular [C#] Web/MVC/SPA
    28. ASP.NET Core with React.js react [C#] Web/MVC/SPA
    29. ASP.NET Core with React.... reactredux [C#] Web/MVC/SPA
    30. Blazor Server App blazorserver [C#] Web/Blazor
    31. Blazor WebAssembly App blazorwasm [C#] Web/Blazor/WebAssembly/PWA
    32. Dotnet 本地工具清单文件 tool-manifest Config
    33. dotnet gitignore 文件 gitignore Config
    34. EditorConfig 文件 editorconfig Config
    35. global.json file globaljson Config
    36. iOS 绑定库(预览) iosbinding [C#] iOS/Mobile
    37. iOS 控制器模板(预览版) ios-controller [C#] iOS/Mobile
    38. iOS 类库(预览) ioslib [C#] iOS/Mobile
    39. iOS 选项卡式应用程序(预... ios-tabbed [C#] iOS/Mobile
    40. iOS 应用程序(预览) ios [C#] iOS/Mobile
    41. MacCatalyst 绑定库(预览) maccatalystbinding [C#] macOS/Mac Catalyst
    42. MacCatalyst 应用程序(预览) maccatalyst [C#] macOS/Mac Catalyst
    43. MSTest Test Project mstest [C#],F#,VB Test/MSTest
    44. MVC ViewImports viewimports [C#] Web/ASP.NET
    45. MVC ViewStart viewstart [C#] Web/ASP.NET
    46. NuGet 配置 nugetconfig Config
    47. NUnit 3 Test Item nunit-test [C#],F#,VB Test/NUnit
    48. NUnit 3 Test Project nunit [C#],F#,VB Test/NUnit
    49. Protocol Buffer File proto Web/gRPC
    50. Razor Class Library razorclasslib [C#] Web/Razor/Library/Razor Class Library
    51. Razor Component razorcomponent [C#] Web/ASP.NET
    52. Razor Page page [C#] Web/ASP.NET
    53. Web 配置 webconfig Config
    54. Windows 窗体控件库 winformscontrollib [C#],VB Common/WinForms
    55. Windows 窗体类库 winformslib [C#],VB Common/WinForms
    56. Windows 窗体应用 winforms [C#],VB Common/WinForms
    57. Worker Service worker [C#],F# Common/Worker/Web
    58. WPF 应用程序 wpf [C#],VB Common/WPF
    59. WPF Class library wpflib [C#],VB Common/WPF
    60. WPF Custom Control Library wpfcustomcontrollib [C#],VB Common/WPF
    61. WPF User Control Library wpfusercontrollib [C#],VB Common/WPF
    62. xUnit Test Project xunit [C#],F#,VB Test/xUnit
    63. PS C:\Users\a-xiaobodou>

    1. PS C:\Users\a-xiaobodou> dotnet new --help
    2. Description:
    3. Template Instantiation Commands for .NET CLI.
    4. Usage:
    5. dotnet new [ [...]] [options]
    6. dotnet new [command] [options]
    7. Arguments:
    8. A short name of the template to create.
    9. Template specific options to use.
    10. Options:
    11. -o, --output Location to place the generated output.
    12. -n, --name The name for the output being created. If no name is specified, the name of the output
    13. directory is used.
    14. --dry-run Displays a summary of what would happen if the given command line were run if it would
    15. result in a template creation.
    16. --force Forces content to be generated even if it would change existing files.
    17. --no-update-check Disables checking for the template package updates when instantiating a template.
    18. --project The project that should be used for context evaluation.
    19. -v, --verbosity Sets the verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], and diag[nostic].
    20. [default: normal]
    21. -d, --diagnostics Enables diagnostic output.
    22. -?, -h, --help Show command line help.
    23. Commands:
    24. create Instantiates a template with given short name. An alias of 'dotnet new