VSCode Build .NET Framework Solution

2025-07-30

筆記如何透過 vscode tasks 設定,在 VSCode 當中進行 .NET Framework 的專案建置

logo

說明

將以下的 Script 加入到資料夾 .vscode/tasks.json,並且將 SolutionName 進行替換。

此外要注意 msbuild 的路徑,這裡假設是 Visual Studio 2022 Professional 版本,若是其他版本請自行調整。

{
	"version": "2.0.0",
	"tasks": [
		{
			"label": "download-nuget",
			"type": "shell",
			"command": "powershell",
			"args": [
				"-Command",
				"if (!(Test-Path 'nuget.exe')) { Write-Host 'Downloading NuGet.exe...'; Invoke-WebRequest -Uri 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe' -OutFile 'nuget.exe' } else { Write-Host 'NuGet.exe already exists.' }"
			],
			"group": "build",
			"isBackground": false,
			"problemMatcher": []
		},
		{
			"label": "restore-packages",
			"type": "shell",
			"command": "powershell",
			"args": [
				"-Command",
				"if (!(Test-Path 'nuget.exe')) { Invoke-WebRequest -Uri 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe' -OutFile 'nuget.exe' }; ./nuget.exe restore SolutionName.sln"
			],
			"group": "build",
			"isBackground": false,
			"problemMatcher": []
		},
		{
			"label": "build",
			"type": "shell",
			"command": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional\\MSBuild\\Current\\Bin\\msbuild.exe",
			"args": [
				"SolutionName.sln",
				"/p:Configuration=Debug",
				"/p:Platform=Any CPU",
				"/verbosity:minimal"
			],
			"group": "build",
			"isBackground": false,
			"problemMatcher": [
				"$msCompile"
			],
			"dependsOn": "restore-packages"
		}
	]
}