VSCode - Shortcut to build the current C# project
Learn how to create a powerful productivity shortcut in Visual Studio Code that builds only the current C# project you’re working on, instead of the entire solution. This guide shows you how to set up a custom Ctrl+B
keybinding that automatically detects the nearest .csproj
file from your active file and builds it quickly. Perfect for large solutions where you want to avoid long build times and focus on testing specific projects during development.
Prerequisites
If you need detailed instructions on installing all the required extensions to run a C# project in VSCode, please refer to the blog post: How to set up C# in VSCode.
📢 UPDATE: Want to make it even easier? Download, extract and import this file CSharp.code-profile, which contains the extensions, settings, and tasks you need to develop in C# with VSCode.
This profile uses PowerShell Core. You can install it using
winget install --id Microsoft.Powershell --source winget
Steps to configure the custom keybinding
You can follow the steps to configure the custom keybinding Ctrl+B
, in VSCode to build the current csproj
project based on the active file.
The steps are:
- Create this PowerShell script to compile the parent
.csproj
:- This script automatically searches for the closest
.csproj
file by traversing the directory tree from the current active file. - If no
.csproj
file is found, it displays an error message. - It provides clear information about what it is compiling.
ℹ️ This script is included (embedded) in the
build-current-project
task below. - This script automatically searches for the closest
- Add a VSCode Task:
- Executes the PowerShell script with the current active file path
- Uses proper problem matchers for error detection
- Configured to work with the workspace environment
- Configure the key shortcut
Ctrl+B
:- Runs the
build-current-project
task when you pressCtrl+B
- Only active when the editor has text focus
- Runs the
1. Script ./.vscode/build-current-project.ps1
:
There is nothing to do in this first step 😁.
You can view this PowerShell script,
which it is embedded into the tasks.json
file to avoid having to create it in the developer’s repo.
2. Add a VSCode Task
Update or create the file ./vscode/tasks.json
with this content:
{
"version": "2.0.0",
"tasks": [
"tasks": [
// Ctrl+Shift+B → To run the default build task (build-solution)
{
"label": "build-solution",
"type": "shell",
"command": "dotnet",
"args": [
"build",
"Microsoft.FluentUI-v5.sln", // 👈 Update this line with your default solution file name
"--configuration",
"Debug"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$msCompile",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
},
"options": {
"cwd": "${workspaceFolder}"
}
},
// Ctrl+B → Build current project based on active file
// PowerShell Core must be installed (see https://github.com/PowerShell/PowerShell)
{
"label": "build-current-project",
"type": "process",
"command": "pwsh",
"args": [
"-NoProfile",
"-Command",
"param([string]$CurrentFile = $env:VSCODE_ACTIVE_FILE, [string]$WorkspaceFolder = $env:WORKSPACE_FOLDER); function Find-NearestProject { param([string]$StartPath); $currentDir = Split-Path -Parent $StartPath; while ($currentDir -and $currentDir -ne [System.IO.Path]::GetPathRoot($currentDir)) { $csprojFiles = Get-ChildItem -Path $currentDir -Filter '*.csproj' -ErrorAction SilentlyContinue; if ($csprojFiles) { return $csprojFiles[0].FullName }; $currentDir = Split-Path -Parent $currentDir }; return $null }; try { if (-not $CurrentFile) { Write-Host 'No active file detected.' -ForegroundColor Red; Write-Host 'Error: No .csproj file found' -ForegroundColor Red; exit 1 } else { Write-Host \"Active file: $CurrentFile\" -ForegroundColor Green; $nearestProject = Find-NearestProject -StartPath $CurrentFile; if ($nearestProject) { $projectPath = $nearestProject; Write-Host \"Found nearest project: $projectPath\" -ForegroundColor Green } else { Write-Host 'Error: No .csproj file found' -ForegroundColor Red; exit 1 } }; Write-Host \"Building: $projectPath\" -ForegroundColor Cyan; & dotnet build \"$projectPath\" --configuration Debug; if ($LASTEXITCODE -eq 0) { Write-Host 'Build completed successfully!' -ForegroundColor Green } else { Write-Host \"Build failed with exit code $LASTEXITCODE\" -ForegroundColor Red; exit $LASTEXITCODE } } catch { Write-Host \"Error: $_\" -ForegroundColor Red; exit 1 }"
],
"group": "build",
"problemMatcher": "$msCompile",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
},
"options": {
"cwd": "${workspaceFolder}",
"env": {
"VSCODE_ACTIVE_FILE": "${file}",
"WORKSPACE_FOLDER": "${workspaceFolder}"
}
}
}
]
}
3. Configure the key shortcut Ctrl+B
If you already installed the Visual Studio Keymap extension, Ctrl+Shift+B
is already configured to compile the entire solution. If this is not the case, you can press these keys and select the task to compile build-solution
.
To configure Ctrl+B
to run the task build-current-project
, created in the previous step, you need to open the keybindings.json
file: go to the search bar at the top of VSCode and search for Open Keyboard Shortcuts (JSON)
.
Update this file with these shortcuts. The first two delete existing shortcuts to allow the use of a new shortcut.
[
{
"key": "ctrl+b",
"command": "-workbench.debug.viewlet.action.addFunctionBreakpointAction"
},
{
"key": "ctrl+b",
"command": "-workbench.action.toggleSidebarVisibility"
}, {
"key": "ctrl+b",
"command": "workbench.action.tasks.runTask",
"args": "build-current-project",
"when": "editorTextFocus"
},
]
Even simpler?
You can download, extract and import this file CSharp.code-profile.
This file contains the extensions, settings, and tasks you need to develop in C# with VSCode.