如何配置VSCode的launch.json与tasks.json调试多C#项目
解决方案:多项目下的VSCode C#配置方案
你有两种可行的配置方式,根据自身使用习惯选择即可:
1. 为每个项目单独配置(直观易上手)
这种方式适合项目数量不多的场景,直接复制现有配置并修改项目相关路径和名称即可,切换项目时直接在启动面板选择对应配置。
修改后的launch.json:
{ "version": "0.2.0", "configurations": [ { "name": ".NET Core Launch (Test01)", "type": "coreclr", "request": "launch", "preLaunchTask": "build Test01", "program": "${workspaceFolder}/Test01/bin/Debug/net7.0/Test01.dll", "args": [], "cwd": "${workspaceFolder}/Test01", "console": "internalConsole", "stopAtEntry": false }, { "name": ".NET Core Launch (Test02)", "type": "coreclr", "request": "launch", "preLaunchTask": "build Test02", "program": "${workspaceFolder}/Test02/bin/Debug/net7.0/Test02.dll", "args": [], "cwd": "${workspaceFolder}/Test02", "console": "internalConsole", "stopAtEntry": false }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach" } ] }
修改后的tasks.json:
{ "version": "2.0.0", "tasks": [ // Test01 相关任务 { "label": "build Test01", "command": "dotnet", "type": "process", "args": [ "build", "${workspaceFolder}/Test01/Test01.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, { "label": "publish Test01", "command": "dotnet", "type": "process", "args": [ "publish", "${workspaceFolder}/Test01/Test01.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, { "label": "watch Test01", "command": "dotnet", "type": "process", "args": [ "watch", "run", "--project", "${workspaceFolder}/Test01/Test01.csproj" ], "problemMatcher": "$msCompile" }, // Test02 相关任务 { "label": "build Test02", "command": "dotnet", "type": "process", "args": [ "build", "${workspaceFolder}/Test02/Test02.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, { "label": "publish Test02", "command": "dotnet", "type": "process", "args": [ "publish", "${workspaceFolder}/Test02/Test02.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, { "label": "watch Test02", "command": "dotnet", "type": "process", "args": [ "watch", "run", "--project", "${workspaceFolder}/Test02/Test02.csproj" ], "problemMatcher": "$msCompile" } ] }
2. 通用配置方案(更高效,适合多项目)
无需为每个项目重复编写配置,利用VSCode的动态命令变量,每次运行时选择要操作的项目,新增项目也不用修改配置文件。
通用版launch.json:
{ "version": "0.2.0", "configurations": [ { "name": ".NET Core Launch (Selected Project)", "type": "coreclr", "request": "launch", "preLaunchTask": "build selected project", "program": "${command:dotnet.getProgram}", "args": [], "cwd": "${command:dotnet.getProjectDirectory}", "console": "internalConsole", "stopAtEntry": false }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach" } ] }
通用版tasks.json:
{ "version": "2.0.0", "tasks": [ { "label": "build selected project", "command": "dotnet", "type": "process", "args": [ "build", "${command:dotnet.getProject}", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, { "label": "publish selected project", "command": "dotnet", "type": "process", "args": [ "publish", "${command:dotnet.getProject}", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, { "label": "watch selected project", "command": "dotnet", "type": "process", "args": [ "watch", "run", "--project", "${command:dotnet.getProject}" ], "problemMatcher": "$msCompile" } ] }
使用该配置时,每次启动调试或执行任务,VSCode会弹出列表让你选择目标项目,无需手动修改路径。
内容的提问来源于stack exchange,提问作者Yan Calvo
相关产品推荐
相关产品推荐

