dotnet new 命令详解

一、简介

dotnet new 命令用于基于指定的模板创建新项目、配置文件、解决方案。

二、常用选项

  • -o, --output <output>:指定创建项目后放置的目录名

示例:

shell 复制代码
dotnet new console -o MyConsoleApp
  • -n, --name <name>:指定项目的名称,不指定则使用项目目录名称

示例:

shell 复制代码
dotnet new console -o MyConsoleApp -n MyConsoleApp1
  • --dry-run:模拟项目创建过程,实际上不创建项目

示例:

shell 复制代码
dotnet new console --dry-run
  • --force:如果项目已经存在,强制覆盖
shell 复制代码
dotnet new console --force
  • -lang, --language {C#|F#|VB}:指定项目使用的语言,项目模板具体支持的语言需查看模板详情,例如:使用 dotnet new list console 可查看console模板支持 C#、F#、VB。

示例:

shell 复制代码
dotnet new console -lang F#
  • -f, --framework <FRAMEWORK>:指定项目使用的目标框架,此选项选择的框架版本会写到项目配置文件中,例如:net6.0、net7.0,项目模板具体支持的框架需查看模板帮助信息,例如:dotnet new console -h 可查看到console模板支持 net6.0、net7.0、net8.0。

示例:

shell 复制代码
dotnet new console -f net8.0
  • -v, --verbosity <LEVEL>:指定命令控制台输出信息的级别,可用的值有:q[uiet]、m[inimal]、n[ormal]、diag[nostic],默认是normal,加括号的意思是可输入首字母或整个单词。

示例:

shell 复制代码
dotnet new console -v m
  • -d, --diagnostics:启动诊断输出。

示例:

shell 复制代码
dotnet new console -d
  • -?, -h, --help:显示命令行帮助信息

示例:

shell 复制代码
dotnet new console -h
  • 查看模板的具体选项,例如:查看console模板的选项
shell 复制代码
dotnet new console -h

-h 这个选项可以作为公共选项,即子命令或子选项都可以使用

例如:dotnet new console -h -lang F#

三、常用子命令

  • dotnet new list:列出可用的模板,如果没有指定模板名,则列出所有模板,没有额外手动安装模板包的情况下,显示的是安装sdk时已经内置好的模板包。

列出所有模板示例:

shell 复制代码
dotnet new list

列出指定模板示例:

shell 复制代码
dotnet new list console
  • dotnet new search:在 NuGet.org上搜索指定的模板

示例:

shell 复制代码
dotnet new search spa
  • dotnet new install:安装指定的模板包,既是包则可以包含多个模板,参数接包id,即上图包名称。

示例:安装上图第一个模板:websharper-spa,使用它的包名称。

shell 复制代码
dotnet new install WebSharper.Templates

可以看到一个包里面包含多个模板。

  • dotnet new uninstall:卸载模板包,如果指定了参数,则卸载指定的模板包,否则列出所有手动安装的模板包。

示例:

shell 复制代码
dotnet new uninstall WebSharper.Templates
  • dotnet new update:检查当前安装的模板包是否有更新,然后安装更新。

示例:

shell 复制代码
dotnet new update

四、如何配置shell使dotnet命令自动补全?

  • zsh shell

~/.zshrc 配置文件中添加以下行:

shell 复制代码
# zsh parameter completion for the dotnet CLI

_dotnet_zsh_complete()
{
  local completions=("$(dotnet complete "$words")")

  # If the completion list is empty, just continue with filename selection
  if [ -z "$completions" ]
  then
    _arguments '*::arguments: _normal'
    return
  fi

  # This is not a variable assignment, don't remove spaces!
  _values = "${(ps:\n:)completions}"
}

compdef _dotnet_zsh_complete dotnet
  • bash shell

~/.bashrc 配置文件中添加以下行:

shell 复制代码
# bash parameter completion for the dotnet CLI

function _dotnet_bash_complete()
{
  local cur="${COMP_WORDS[COMP_CWORD]}" IFS=$'\n' # On Windows you may need to use use IFS=$'\r\n'
  local candidates

  read -d '' -ra candidates < <(dotnet complete --position "${COMP_POINT}" "${COMP_LINE}" 2>/dev/null)

  read -d '' -ra COMPREPLY < <(compgen -W "${candidates[*]:-}" -- "$cur")
}

complete -f -F _dotnet_bash_complete dotnet
相关推荐
一丝晨光2 小时前
Java、PHP、ASP、JSP、Kotlin、.NET、Go
java·kotlin·go·php·.net·jsp·asp
yufei-coder3 小时前
C#基础语法
开发语言·c#·.net
Mudrock__4 小时前
前后端传输文件(图片)
vue·.net
时光追逐者1 天前
WaterCloud:一套基于.NET 8.0 + LayUI的快速开发框架,完全开源免费!
前端·microsoft·开源·c#·.net·layui·.netcore
friklogff1 天前
【C#生态园】打造现代化跨平台应用:深度解析.NET桌面应用工具
开发语言·c#·.net
@Unity打怪升级2 天前
【C#】CacheManager:高效的 .NET 缓存管理库
开发语言·后端·机器学习·缓存·c#·.net·.netcore
yufei-coder3 天前
.Net 9与AI开发
人工智能·.net
孟章豪3 天前
深入理解.NET中的委托与事件:实现灵活的事件驱动编程
.net
小彰5 天前
.Net 6.0 Windows平台如何判断当前电脑是否联网
windows·.net
小码编匠5 天前
.NET 中的表达式树(Expression Trees)
后端·c#·.net