25

When building my C# project, how can I switch to Release configuration in VS Code?

Right now I launch my application with Ctrl+F5 or Debug -> Start Without Debugging which also builds it, but this creates only a debug build at bin/Debug. There is no Build menu in VS Code.

Here's my tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/dotnetcore-test.csproj"
            ],
            "problemMatcher": "$msCompile"
        }
    ]
}
sashoalm
  • 69,127
  • 105
  • 396
  • 720

4 Answers4

29

edit the task.json like this:

{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build Debug",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/dotnetcore-test.csproj"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "taskName": "build Release",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/dotnetcore-test.csproj",
                "-c",
                "Release"
            ],
            "problemMatcher": "$msCompile"
        }        
    ]
}

then when you press Ctrl+Shift+B the Command Palette will let you choose between Build Release and Build Debug

source: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-build?tabs=netcore2x

V Bota
  • 467
  • 6
  • 11
  • I can see the permutations quickly getting out of hand. Maybe some config variables could be used instead: https://stackoverflow.com/questions/44303316/vscode-defining-own-variables-in-tasks-json – jozxyqk Feb 10 '21 at 06:07
  • As of 18th Sept, 2021 and VS Code: 1.60.0. I had the same issue and it turned out to be I wasn't having `"isBuildCommand": true` in both configs due to which I got an error `No build tasks defined`. Check this - https://stackoverflow.com/a/69232604/4317232 – Koder101 Sep 18 '21 at 07:47
7

You can perform a release build via the terminal with:

dotnet build -c release

If you want to run in release, use:

dotnet run -c release

Source: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-build

2

you can just do that without tasks.json , but with Launch.json

Here is a configuration I always use to build my C# projects by F5 : Gist

Parsa
  • 3,306
  • 1
  • 22
  • 32
0

Terminal -> Run Task... -> select the task name to run

John_J
  • 1,751
  • 22
  • 23