Skip to content

.NET Core 实战 [No.7~13] 命令行工具

开发时一般都是通过 Visual Studio 来创建和运行程序,不过了解一下常用 .NET Core 的命令行工具还是有必要的。特别是编译和运行命令,偶尔会使用到。

1. dotnet new

下面的命令会创建一个控制台项目,项目名默认和当前文件夹同相同。

bash
dotnet new console

通过 -n 参数可以指定新项目的名称。

bash
dotnet new console -n ProjectName

项目会自动在当前目录下的同名文件夹下。

bash
└─ProjectName
  Program.cs
  ProjectName.csproj

    └─obj
            project.assets.json
            ProjectName.csproj.nuget.cache
            ProjectName.csproj.nuget.dgspec.json
            ProjectName.csproj.nuget.g.props
            ProjectName.csproj.nuget.g.targets

通过 -o 参数可以指定新项目的存放位置。

bash
dotnet new console -o Sample

如果未通过 -n 参数指定项目名,则默认和项目存放位置相同。

bash
└─Sample
  Program.cs
  Sample.csproj

   └─obj
           project.assets.json
           Sample.csproj.nuget.cache
           Sample.csproj.nuget.dgspec.json
           Sample.csproj.nuget.g.props
           Sample.csproj.nuget.g.targets

通常 -o-n 参数会同时使用。

2. dotnet build

该命令编译应用程序项目。

bash
dotnet build App1\App1.csproj

编译前的项目目录结构:

bash
└─App1
  App1.csproj
  Program.cs

    └─obj
            App1.csproj.nuget.cache
            App1.csproj.nuget.dgspec.json
            App1.csproj.nuget.g.props
            App1.csproj.nuget.g.targets
            project.assets.json

编译后的项目目录结构:

bash
└─App1
  App1.csproj
  Program.cs

    ├─bin
  └─Debug
      └─netcoreapp3.1
              App1.deps.json
              App1.dll
              App1.exe
              App1.pdb
              App1.runtimeconfig.dev.json
              App1.runtimeconfig.json

    └─obj
  App1.csproj.nuget.cache
  App1.csproj.nuget.dgspec.json
  App1.csproj.nuget.g.props
  App1.csproj.nuget.g.targets
  project.assets.json

        └─Debug
            └─netcoreapp3.1
                    App1.AssemblyInfo.cs
                    App1.AssemblyInfoInputs.cache
                    App1.assets.cache
                    App1.csproj.FileListAbsolute.txt
                    App1.csprojAssemblyReference.cache
                    App1.dll
                    App1.exe
                    App1.pdb

可以看出在 binobj 目录下都新增了 Debug 目录。
由于我的开发环境当前默认的 .NET Core 版本为 3.1,所以编译结果文件存放在 netcoreapp3.1 目录下。

通过添加 -c Release 参数可以编译项目的 Release 版本。

bash
dotnet build App1\App1.csproj -c Release

编译后在 binobj 目录下都新增了一个 Release 目录。

bash
└─App1
  App1.csproj
  Program.cs

    ├─bin
  ├─Debug
  └─netcoreapp3.1
          App1.deps.json
          App1.dll
          App1.exe
          App1.pdb
          App1.runtimeconfig.dev.json
          App1.runtimeconfig.json

  └─Release
      └─netcoreapp3.1
              App1.deps.json
              App1.dll
              App1.exe
              App1.pdb
              App1.runtimeconfig.dev.json
              App1.runtimeconfig.json

    └─obj
  App1.csproj.nuget.cache
  App1.csproj.nuget.dgspec.json
  App1.csproj.nuget.g.props
  App1.csproj.nuget.g.targets
  project.assets.json

        ├─Debug
  └─netcoreapp3.1
          App1.AssemblyInfo.cs
          App1.AssemblyInfoInputs.cache
          App1.assets.cache
          App1.csproj.FileListAbsolute.txt
          App1.csprojAssemblyReference.cache
          App1.dll
          App1.exe
          App1.pdb

        └─Release
            └─netcoreapp3.1
                    App1.AssemblyInfo.cs
                    App1.AssemblyInfoInputs.cache
                    App1.assets.cache
                    App1.csproj.FileListAbsolute.txt
                    App1.csprojAssemblyReference.cache
                    App1.dll
                    App1.exe
                    App1.pdb

3. dotnet new sln

该命令创建解决方案文件,使用 -n 参数可以指定解决方案文件名。

bash
dotnet new sln -n Happy

dotnet sln add

使用 dotnet sln {SolutionName} add {ProjectName1} [{ProjectName2}] 命令将项目添加到解决方案中。

bash
dotnet sln Happy.sln add demo1\demo1.csproj demo2\demo2.csproj

执行后解决方案文件中会添加如下内容:

csharp
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "demo1", "demo1\demo1.csproj", "{E3B59553-1924-481B-9594-9D7C94473879}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "demo2", "demo2\demo2.csproj", "{98DBAA15-163C-4601-A678-038247B82862}"
EndProject

dotnet sln list

该命令可以查看解决方案中的项目列表。

bash
>dotnet sln list
项目
--
demo1\demo1.csproj
demo2\demo2.csproj

dotnet sln remove

该命令可以从解决方案中移除项目。

bash
dotnet sln remove demo1\demo1.csproj

4. dotnet {.dll}

该命令可以运行指定的 dll 文件。

bash
>dotnet bin\Debug\netcoreapp3.1\ProjectName.dll

Hello World!

5. dotnet run

该命令运行当前目录下的项目。

bash
>dotnet run
Hello World!

参考:《.NET Core 实战:手把手教你掌握 380 个精彩案例》 -- 周家安 著


Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.