VSCode搭建 .netcore 开发环境

20 篇文章 0 订阅
7 篇文章 0 订阅

一、MacOS

笔者笔记本电脑上安装的是macOS High Sierra(10.13),想要尝试一下新版本的.netcore,之前系统是10.12时,.netcore 3.1刚出来时安装过3.1版本,很久没更新了,最近.net8出来了,想试一下,但是需要更新系统,于是安装了.netcore7,它的支持时间到2024年5月14日。

下载了.netcore7后,直接双击安装即可。安装后的路径为:/usr/local/share/dotnet

笔者VSCode的版本为1.85.1,需要安装C#插件,目前最新版本为2.14.8,但macOS 10.13目前最高只能安装v1.26.0版本的C#插件,不能安装更高的版本,否则不能调试。

在这里插入图片描述

另外还需要安装一个.NET Install Tool插件:

在这里插入图片描述
这样就可以在终端使用dotnet命令创建项目了,比如创建一个控制台应用:

$ dotnet new console
已成功创建模板“控制台应用”。

正在处理创建后操作...
正在还原 /Users/witton/Projects/cc/cc.csproj:
  正在确定要还原的项目…
  已还原 /Users/witton/Projects/cc/cc.csproj (用时 120 ms)。
已成功还原。

此时打开生成的Program.cs,并打上断点,切换到运行和调试工具栏,执行Generate C# Assets for Build and Debug,插件会自动生成launch.jsontasks.json两个文件:

在这里插入图片描述
launch.json

{
	"version": "0.2.0",
	"configurations": [
		{
			// Use IntelliSense to find out which attributes exist for C# debugging
			// Use hover for the description of the existing attributes
			// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md
			"name": ".NET Core Launch (console)",
			"type": "coreclr",
			"request": "launch",
			"preLaunchTask": "build",
			// If you have changed target frameworks, make sure to update the program path.
			"program": "${workspaceFolder}/bin/Debug/net7.0/cc.dll",
			"args": [],
			"cwd": "${workspaceFolder}",
			// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
			"console": "internalConsole",
			"stopAtEntry": false
		},
		{
			"name": ".NET Core Attach",
			"type": "coreclr",
			"request": "attach"
		}
	]
}

tasks.json:

{
	"version": "2.0.0",
	"tasks": [
		{
			"label": "build",
			"command": "dotnet",
			"type": "process",
			"args": [
				"build",
				"${workspaceFolder}/cc.csproj",
				"/property:GenerateFullPaths=true",
				"/consoleloggerparameters:NoSummary"
			],
			"problemMatcher": "$msCompile"
		},
		{
			"label": "publish",
			"command": "dotnet",
			"type": "process",
			"args": [
				"publish",
				"${workspaceFolder}/cc.csproj",
				"/property:GenerateFullPaths=true",
				"/consoleloggerparameters:NoSummary"
			],
			"problemMatcher": "$msCompile"
		},
		{
			"label": "watch",
			"command": "dotnet",
			"type": "process",
			"args": [
				"watch",
				"run",
				"--project",
				"${workspaceFolder}/cc.csproj"
			],
			"problemMatcher": "$msCompile"
		}
	]
}

此时按F5运行启动调试即可开始调试C#程序:

在这里插入图片描述
微软开发了一个新的插件C# Dev Kit,它提供了更加强大的功能,并且可以像VS那样使用解决方案资源管理器来管理项目。该插件需要C#插件为2.0及以上版本,但由于笔者的MacOS系统比较老了,C#插件2.0及以上版本不能进行调试,所以只能使用1.X来将就使用了。

如果是macOS 10.15及以上版本,就可以直接安装.net8了,VSCode插件也可以直接安装C# Dev Kit以及IntelliCode for C# Dev Kit插件了。但是由于.NET Install Tool插件目前仅支持7.x版本的.net,所以还需要安装.net 7runtime:runtime-7.0.14-macos-x64-installer

安装好后就可以使用F1调出.NET:新建项目...命令,来新建.net项目了:

在这里插入图片描述

选择项目模板:

在这里插入图片描述

比如选择控制台应用,创建好项目后,可以看到解决方案资源管理器

在这里插入图片描述

二、Windows

如果是Windows10,则可以直接安装最新的.net 8,并且安装上C# Dev Kit以及IntelliCode for C# Dev Kit,也不需要像macOS那样还需要安装.net 7runtime

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值