一、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#插件,不能安装更高的版本,否则不能调试。

![在这里插入图片描述](../assets/2024-01-08-VSCode搭建 .netcore 开发环境/924dc8987d18a33cd41bbbc7c37f5385.png)

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

![在这里插入图片描述](../assets/2024-01-08-VSCode搭建 .netcore 开发环境/f9cc78d85d4719a3e2b516e376fc283a.png) 这样就可以在终端使用dotnet命令创建项目了,比如创建一个控制台应用:

1$ dotnet new console
2已成功创建模板“控制台应用”。
3
4正在处理创建后操作...
5正在还原 /Users/witton/Projects/cc/cc.csproj:
6  正在确定要还原的项目…
7  已还原 /Users/witton/Projects/cc/cc.csproj (用时 120 ms)8已成功还原。

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

![在这里插入图片描述](../assets/2024-01-08-VSCode搭建 .netcore 开发环境/df70e93eb1dc006d1229ebc18c474ade.png) launch.json

 1{
 2	"version": "0.2.0",
 3	"configurations": [
 4		{
 5			// Use IntelliSense to find out which attributes exist for C# debugging
 6			// Use hover for the description of the existing attributes
 7			// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md
 8			"name": ".NET Core Launch (console)",
 9			"type": "coreclr",
10			"request": "launch",
11			"preLaunchTask": "build",
12			// If you have changed target frameworks, make sure to update the program path.
13			"program": "${workspaceFolder}/bin/Debug/net7.0/cc.dll",
14			"args": [],
15			"cwd": "${workspaceFolder}",
16			// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
17			"console": "internalConsole",
18			"stopAtEntry": false
19		},
20		{
21			"name": ".NET Core Attach",
22			"type": "coreclr",
23			"request": "attach"
24		}
25	]
26}

tasks.json:

 1{
 2	"version": "2.0.0",
 3	"tasks": [
 4		{
 5			"label": "build",
 6			"command": "dotnet",
 7			"type": "process",
 8			"args": [
 9				"build",
10				"${workspaceFolder}/cc.csproj",
11				"/property:GenerateFullPaths=true",
12				"/consoleloggerparameters:NoSummary"
13			],
14			"problemMatcher": "$msCompile"
15		},
16		{
17			"label": "publish",
18			"command": "dotnet",
19			"type": "process",
20			"args": [
21				"publish",
22				"${workspaceFolder}/cc.csproj",
23				"/property:GenerateFullPaths=true",
24				"/consoleloggerparameters:NoSummary"
25			],
26			"problemMatcher": "$msCompile"
27		},
28		{
29			"label": "watch",
30			"command": "dotnet",
31			"type": "process",
32			"args": [
33				"watch",
34				"run",
35				"--project",
36				"${workspaceFolder}/cc.csproj"
37			],
38			"problemMatcher": "$msCompile"
39		}
40	]
41}

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

![在这里插入图片描述](../assets/2024-01-08-VSCode搭建 .netcore 开发环境/fbd090be83a89376bc19b6a4e5c5c8bd.png) 微软开发了一个新的插件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项目了:

![在这里插入图片描述](../assets/2024-01-08-VSCode搭建 .netcore 开发环境/11d8f5140db3dbb95ac1610f13a69452.png)

选择项目模板:

![在这里插入图片描述](../assets/2024-01-08-VSCode搭建 .netcore 开发环境/82525ad552fa03272be46387e2d3ccd4.png)

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

![在这里插入图片描述](../assets/2024-01-08-VSCode搭建 .netcore 开发环境/a08c6d822f22d95f53aa1efaae0d0827.png)

二、Windows

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